Reputation: 1672
I have the following IHttpHandler (ashx) handler:
public class UploadFile : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
using (var fileStream = File.OpenWrite(@"C:\" + context.Request.QueryString["FileName"]))
{
context.Request.InputStream.CopyTo(fileStream);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
Which is called from:
$.ajax({
type: "POST",
url: "UploadFile.ashx?FileName=" + file.name,
data: file,
success: function () {
alert("Success");
},
processData: false,
contentType: file.type
});
The file variable comes from a <input type="file" />
and is retrieved using document.getElementById("fuExcelFile").files[0]
.
This work with seemingly every file other than xlsx. With xlsx, the file is uploaded but when I open it in Excel I get the following error:
We found a problem with some content in '.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, Click Yes
I'm not sure where to go from here since it works with other files :S
EDIT: Here is the file im testing with and a image of it in the hex editor
Upvotes: 1
Views: 342
Reputation: 171178
This was a hard one.
In the discussion in the comment we found out that the destination file is larger then the source. Excel could still open it but issued warnings. This smelled like a partial corruption.
Turns out File.OpenWrite
appends to an existing file. During testing you have appended the same file over and over.
The key to debugging this was to binary compare the files. When you're stuck with a bug and don't know what it is: Investigate! Experiment. Obtain new information. Vary the setup.
Upvotes: 1