0

我正在尝试使用 iframe hack 来模拟 Ajax 查询,我可以在其中将文件发送到服务器。它与 IE (7, 8, 9) 完美配合。我的 ASP.NET 服务器接收文件并可以读取其内容。

但是当我尝试在 Chrome 或 Firefox 中使用相同的表单时,ASP.NET 服务器仍在接收文件(count = 1),但它是空的(contentLength = 0)。有了Fiddler,我在用IE的时候可以看到文件的内容,也可以看到Chrome和Firefox发来的空文件。

这是我的简化 HTML

<form id="importForm" method="post" enctype="multipart/form-data">
  <input type="file" id="importFileUpload" name="importFileUpload" class="importFileUpload" />
  <input id="importNewListButton" type="button" class="importexportButton" value="send it" />
</form>
<iframe id="importUploadFileIframe" name="importUploadFileIframe" src="" style="width:0;height:0;border:0px solid #fff;display:none;"></iframe>

这是我的简化 jQuery

$("form#importForm").attr("action", "myScript.aspx");
$("form#importForm").attr("target", "importUploadFileIframe");
$("form#importForm").submit();

这是我的简化 C#

HttpPostedFile file = Request.Files[0];
Helper.log("file = " + file.FileName);
Helper.log("file = " + file.ContentType);
Helper.log("file = " + file.ContentLength);
4

1 回答 1

0

我刚刚发现使用以下代码在表单上触发的事件(在提交之前)是问题所在:

$("form#importForm").ready(function(){ ... });

If I remove this event, Chrome and Firefox (and still IE) will receive my file. Hope this could help somebody.

于 2012-09-12T19:51:57.323 回答