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.
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);
}
See more on this question at Stackoverflow