【发布时间】:2011-01-07 16:22:15
【问题描述】:
我目前正在开发一个 AJAX 文件上传脚本,它在 Firefox 中就像一个魅力,但在 IE 中不起作用。
这是我正在使用的基本 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();
}
一旦 iframe 完成发布/加载,finishedPostingFile() 将是回调函数。
现在,这在 Firefox 中就像一个魅力,但在 IE 中不起作用。我已经发现 IE 需要 attr('encoding',...) 而不是 attr('enctype',...) 并且我还尝试了它,而无需通过将这些元素编写为纯 html 来动态创建表单和 iframe,这并没有真正的区别。
IE(IE8,具体来说,没有在
【问题讨论】:
-
你有没有得到一个很好的答案?
标签: jquery ajax iframe file-upload cross-browser