我們知道該文件不能通過ajax上傳,但我不明白爲什麼這ajax upload file plugin使用普通的ajax?這個插件爲什麼可以通過ajax上傳文件?
_upload: function(id, params){
var file = this._files[id],
name = this.getName(id),
size = this.getSize(id);
this._loaded[id] = 0;
var xhr = this._xhrs[id] = new XMLHttpRequest();
var self = this;
xhr.upload.onprogress = function(e){
if (e.lengthComputable){
self._loaded[id] = e.loaded;
self._options.onProgress(id, name, e.loaded, e.total);
}
};
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
self._onComplete(id, xhr);
}
};
// build query string
params = params || {};
params['qqfile'] = name;
var queryString = qq.obj2url(params, this._options.action);
xhr.open("POST", queryString, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("X-File-Name", encodeURIComponent(name));
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.send(file);
}
我不喜歡這個插件,因爲你不能用jQuery和目標元素中使用它必須是一個ID,
function createUploader(){
var uploader = new qq.FileUploader({
element: document.getElementById('file-uploader-demo1'),
action: 'upload-file.php',
onComplete: function(id, fileName, responseJSON){alert(responseJSON[0].filename)},
debug: true
});
}
// in your app create uploader as soon as the DOM is ready
// don't wait for the window to load
window.onload = createUploader;
這將是我所需要的,如果我可以這樣做這與jquery,但我只是不知道如何改變源代碼,因爲它是用純JavaScript編寫的!
element: $('.upload'), or element: $('#upload'),
如果我沒有弄錯,更新的瀏覽器(除了原因的IE瀏覽器,部分webkit也是我認爲的),只能用ajax上傳文件。這是由於javascript/html5中的新文件api。哦,順便說一下,你知道jQuery **是** javascript,對吧?所以說你不能閱讀它,因爲它是純javascript,而不是jQuery只是沒有任何意義: -/ – Alxandr
大聲笑它需要更長的時間來理解純js比jquery ... :-)像普通的js ajax code - 我真的不明白它...... :-( – laukok