我目前正在研究一個AJAX文件上傳腳本,它在Firefox中像一個魅力一樣工作,但在IE中不起作用。jQuery AJAX Fileupload crossbrowser支持
這是我使用的基本HTML:
<form >
<input type="file" name="FileFields" id="FileFields"/>
<button type="button" onclick="uploadFile();" id="uploadButton">Upload</button>
<ul id="files"/>
... other form elements ...
</form>
<div id="fileUploadDiv"/>
這是uploadFile功能:
function uploadFile()
{
//we don't want more then 5 files uploaded
if($('#files li').size() >= 5)
{
return;
}
//disable the upload button
$('#uploadButton').attr('disabled','disabled');
//show loading animation
$('#files').append(
$('<li>')
.attr('id','loading')
.append(
$('<img>').attr('src','/images/loading.gif')
)
.addClass('loading')
);
//add all neccessary elements (the form and the iframe)
$('#fileUploadDiv').append(
$('<form action="/uploadFile" method="post" id="fileUploadForm">')
.attr('enctype','multipart/form-data')
.attr('encoding', 'multipart/form-data')
.attr('target', 'upload_frame')
.append(
$('#FileFields').clone()
.css('visibility','hidden')
)
.append(
$('<iframe>').attr('name','upload_frame')
.load(function(){finishedPostingFile();})
.attr('id','upload_frame')
.attr('src','')
.css({
'width':'0px',
'height':'0px',
'border':'0px none #fff'
})
)
);
//start uploading the file
$('#fileUploadForm').submit();
}
和finishedPostingFile()將回調函數,一旦iframe中已完成發佈/加載。
現在,這工作像在Firefox的魅力,但在IE中不起作用。我已經知道IE需要attr('encoding',...)
而不是attr('enctype',...)
,我也嘗試通過將這些元素編寫爲純HTML來創建表單和iframe,而這些元素並沒有真正起作用。 (IE8,具體來說,沒有在< 8中測試過)沒有給出錯誤,加載動畫只是繼續旋轉,沒有文件被上傳... 任何人都有任何想法關於如何使這項工作?
你有沒有得到一個很好的答案呢? – camainc 2011-05-19 15:03:16