I’m trying to use the iframe hack to simulate an Ajax query where I can send a file to the server. It is working perfectly with IE (7, 8, 9). My ASP.NET server receive the file and can read its content.
But when I try to use the same form with Chrome or Firefox, the ASP.NET Server is still receiving a file (count = 1), but it is empty (contentLength = 0). With Fiddler, I can see the content of the file when I’m using IE, and I can see the empty file sent from Chrome and Firefox.
Here is my simplified 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>
Here is my simplified jQuery
$("form#importForm").attr("action", "myScript.aspx");
$("form#importForm").attr("target", "importUploadFileIframe");
$("form#importForm").submit();
And here is my simplified C#
HttpPostedFile file = Request.Files[0];
Helper.log("file = " + file.FileName);
Helper.log("file = " + file.ContentType);
Helper.log("file = " + file.ContentLength);
I just found out that an event I was triggering on my form (before submitting it) with the following code was the problem :
If I remove this event, Chrome and Firefox (and still IE) will receive my file. Hope this could help somebody.