jQuery / iframe文件上传解决方案的问题

时间:2011-05-06 13:45:56

标签: jquery asp.net asp.net-mvc file-upload uploading

我最近为文件上传实现了一个jQuery / iframe解决方案,并且遇到了一个相当恼人的问题(当然只在IE中)。

点击我的上传按钮并选择要上传的文件后,我会自动将包含该文件的表单提交给我的iframe。但是 - 我需要单击页面上的任意位置,以便提交处理并在我的控制器中触发操作。

我认为这是一个问题,iframe获得了关注,这似乎是停止'更改'功能完成执行,但我不完全确定。 iframe可能需要某种类型的onload函数才能将焦点重新放回原始页面继续执行?

代码:

表格&文件上传:

<form id="attachForm" action="<%= Url.Action("TestUpload","Images") %>" 
      method="post"  enctype="multipart/form-data" target='upload_target' >
        <input id='actualupload' type='file' multiple="" name='file' />
</form>
<iframe id='upload_target' name='upload_target' src='' 
        style="width:0; height: 0; border: 0;" frameborder="0"></iframe>

jQuery提交表单:

$("#actualupload").live('change',function()
{
    $("#attachForm").submit();
    alert("Fired!"); //This only occurs after clicking on the screen after the 
                     //file selection.
});

1 个答案:

答案 0 :(得分:3)

似乎.live()导致了这种奇怪的行为。如果你使用ie。

$(function() {
    $("#actualupload").change( function()
    {
        $("#attachForm").submit();
        alert("Fired!"); //This only occurs after clicking on the screen after the 
                         //file selection.
    });
});

它也适用于IE。