我想抓取上传的文件<input type='file'>
tag.
当我执行 $('#inputId').val() 时,它只获取name文件的名称,而不是实际的文件本身。
我正在尝试遵循这个:
http://hacks.mozilla.org/2011/03/the-shortest-image-uploader-ever/ http://hacks.mozilla.org/2011/03/the-shortest-image-uploader-ever/
function upload(file) {
// file is from a <input> tag or from Drag'n Drop
// Is the file an image?
if (!file || !file.type.match(/image.*/)) return;
// It is!
// Let's build a FormData object
var fd = new FormData();
fd.append("image", file); // Append the file
fd.append("key", "6528448c258cff474ca9701c5bab6927");
// Get your own key: http://api.imgur.com/
// Create the XHR (Cross-Domain XHR FTW!!!)
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://api.imgur.com/2/upload.json"); // Boooom!
xhr.onload = function() {
// Big win!
// The URL of the image is:
JSON.parse(xhr.responseText).upload.links.imgur_page;
}
// Ok, I don't handle the errors. An exercice for the reader.
// And now, we send the formdata
xhr.send(fd);
}
Use event.target.files
for change
检索 File 实例的事件。
$('#inputId').change(function(e) {
var files = e.target.files;
for (var i = 0, file; file = files[i]; i++) {
console.log(file);
}
});
请查看此处了解更多信息:http://www.html5rocks.com/en/tutorials/file/dndfiles/ http://www.html5rocks.com/en/tutorials/file/dndfiles/
此解决方案使用并非所有浏览器都支持的文件 API - 请参阅http://caniuse.com/#feat=fileapi http://caniuse.com/#feat=fileapi .
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)