0

I am using FileUpload control to upload multiple files and it does successfully but problem is that when I download files after uploading then it becomes corrupt i.e. .pdf, .docx etc. Upon inspection I found that the problem is with upload control because I checked file in server directory after uploading and tried opening it but files throws error i.e. file is corrupt.

I checked every the uploaded file in server directory, it doesn't work there too, so the problem is that it uploads incorrectly

protected void UploadIncomingLetterMaterial(int CaseLetterID)
{
    if (FileUpload1.HasFiles)
    {

        foreach (HttpPostedFile uploadedfiles in FileUpload1.PostedFiles)
        {
            string fileName = Path.GetFileName(uploadedfiles.FileName);
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/SiteImages/") + fileName);

            mngUploadedMaterialIncomingLetters.InsertUploadedMaterialIncomingLetters(fileName, "", CaseLetterID);
        }

UPDATE:

The problem is that when i upload multiple files then it makes the size of all files same. Same to the size of first file in loop.

  • Your question is really unclear at the moment. What do you mean by "extensions are correct but still doesn't"? And what does "it was opening even sizes are same" mean? You haven't explained what makes you think the files are corrupt - and your title suggests you think the file upload control *doesn't* corrupt files... – Jon Skeet Feb 03 '16 at 07:47
  • you are getting file with proper extension ? Also checked the file size in server (uploaded) and original one ? – Rigin Feb 03 '16 at 07:47
  • @AlexeiLevenkov check updated question, i edited between lines – user5820210 Feb 03 '16 at 07:52
  • @JonSkeet check updated question, i changed title and details – user5820210 Feb 03 '16 at 07:53
  • @JonSkeet i just checked again, the problem is that when I upload multiple files then it makes the size of all files same i.e. same as the size of first uploaded files – user5820210 Feb 03 '16 at 07:59
  • Right - that's due to `FileUpload1.PostedFile.SaveAs` I suspect... – Jon Skeet Feb 03 '16 at 07:59
  • @Rigin i just checked, the problem is that it makes the size of all files same. Same to the size of first file in loop – user5820210 Feb 03 '16 at 07:59
  • Can you post your complete needed aspx and cs file – Rigin Feb 03 '16 at 09:21

1 Answers1

1

It looks like this is the problem:

FileUpload1.PostedFile.SaveAs(Server.MapPath("~/SiteImages/") + fileName);

That's saving the first file multiple times - you're trying to save the file you're currently referring to as uploadedfiles (which should be singular). It looks like you should have:

foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
{
    string fileName = Path.GetFileName(uploadedFile.FileName);
    uploadedFile.SaveAs(Server.MapPath("~/SiteImages/") + fileName);
    mngUploadedMaterialIncomingLetters.InsertUploadedMaterialIncomingLetters(fileName, "", CaseLetterID);
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • let me try sir wait sir – user5820210 Feb 03 '16 at 08:03
  • @user5820210: That seems very unlikely to me. I *strongly* suspect that you haven't actually rebuilt, or something similar. That seems a much more likely explanation for the problem (given that the existing behaviour is explained by your bug) than that a control used by many people is fundamentally flawed. – Jon Skeet Feb 03 '16 at 08:08
  • I rebuilt the website and ran but still same problem – user5820210 Feb 03 '16 at 08:12
  • @user5820210: I suggest you add some diagnostics that prove that you're actually running the new code - e.g. include "newcode" in the name of the file that gets saved. I still believe it's much more likely that you're not *really* running the new code than that you've discovered a fatal bug in the control. – Jon Skeet Feb 03 '16 at 08:16
  • @user5820210: Then you should ask a separate question for that - these are two separate problems, and you should only ask about one problem per post. (And give more information than just "makes them corrupt" - what is in the files after you've downloaded them? Is the size correct? Are they always a bit longer? etc...) – Jon Skeet Feb 03 '16 at 10:22
  • @user5820210: No, post a new *question*. – Jon Skeet Feb 03 '16 at 10:24
  • DONE, http://stackoverflow.com/questions/35174404/why-doesn-response-transmitfile-corrupt-the-downloaded-file – user5820210 Feb 03 '16 at 10:26
  • @user5820210: Right, so now remove the final part of *this* question, so that it reflects just the one problem. – Jon Skeet Feb 03 '16 at 10:26