Abu*_*yah 0 html javascript jquery
我需要在使用jquery上传之前显示上传图片,但在Chrome路径中显示如下虚假路径:
C:\fakepath\myImage.jpg
Run Code Online (Sandbox Code Playgroud)
在Firefox中,它只显示文件名:
myImage.jpg
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能在上传之前预览我的图像文件
<input type="file">
<img src="" class="preview" />
Run Code Online (Sandbox Code Playgroud)
如果这是不可能的......那么HTML5呢?
我不知道jQuery函数(抱歉),但HTML5可以实现,大多数浏览器都支持HTML5函数.我在HTML5rocks(1)中找到了一段代码:
<style>
.thumb {
height: 75px;
border: 1px solid #000;
margin: 10px 5px 0 0;
}
</style>
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>
<script>
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML = ['<img class="thumb" src="', e.target.result,
'" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
Run Code Online (Sandbox Code Playgroud)
祝好运 :)
(1)HTML5rocks - 阅读文件:http://www.html5rocks.com/en/tutorials/file/dndfiles/