Tôi muốn lấy tệp được tải lên trong thẻ <input type='file'>
.jQuery lấy một tệp được tải lên với loại đầu vào = 'tệp'
Khi tôi thực hiện $ ('# inputId'). Val(), nó chỉ lấy tên của tệp, chứ không phải chính tệp thực sự.
Tôi đang cố gắng làm theo điều này:
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);
}
Thông tin bổ sung: Một tệp được tải lên luôn nằm trong 'e.target.files [0]', tại thời điểm đó bạn không cần vòng lặp 'for'. – DanFromGermany