Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 30, 2012 01:17 PM by RDowdall
Member
361 Points
215 Posts
Nov 26, 2012 06:05 PM|LINK
Hello - I am uploading pdf files to a Sql server 2005 database. It seems to go well until I try to download and view the pdfs.
I get the following error from Adobe Acrobat: There was an error opening this document. The file is damaged and could not be repaired.
My code is below. Does anyone have an idea about what is causing this?
Thanks,
RD
protected void UploadFile(object src, EventArgs e) { if (myFile.HasFile) { string fileName = Path.GetFileName(myFile.PostedFile.FileName); string fileExtension = Path.GetExtension(myFile.PostedFile.FileName); string documentType = string.Empty; switch (fileExtension) { case ".pdf": documentType = "application/pdf"; break; case ".xls": documentType = "application/vnd.ms-excel"; break; case ".xlsx": documentType = "application/vnd.ms-excel"; break; case ".docx": documentType = "application/vnd.ms-word"; break; case ".doc": documentType = "application/vnd.ms-word"; break; case "gif": documentType = "image/gif"; break; case "png": documentType = "image/png"; break; case "jpg": documentType = "image/jpg"; break; } int fileSize = myFile.PostedFile.ContentLength; byte[] documentBinary = new byte[fileSize]; myFile.PostedFile.InputStream.Read(documentBinary, 0, fileSize); Document doc = new Document(); doc.App_ID = Convert.ToInt32(ViewState["App_ID"]); doc.file_type = documentType; doc.file_size = fileSize; doc.file_name = fileName.ToString(); doc.uploaded_by = person; doc.uploaded_dt = DateTime.Now; doc.upload_doc = documentBinary; db.Documents.InsertOnSubmit(doc); try { db.SubmitChanges(); } catch (Exception ex) { ErrorHandler.LogException(ex.Message.ToString(), "Uploads.aspx", "UpLoadFile",Convert.ToInt32(doc.App_ID)); } } }
All-Star
134511 Points
21576 Posts
Moderator
MVP
Nov 27, 2012 03:39 AM|LINK
compare the bytes of file downloaded with the bytes of file uploaded.
23373 Points
2490 Posts
Microsoft
Nov 30, 2012 05:47 AM|LINK
Hi,
In order to resolve your issue, you need to check whether you have uploaded it correctly at first. Then I would like to suggest you to check the link below for the similar thread which provides some solutions for the same issue.
http://stackoverflow.com/questions/2440518/the-file-is-damaged-and-could-not-be-repaired
In addition, you can follow the article which describes about 4 ways to send a PDF file to the IE Client in ASP.NET .
Best wishes,
Nov 30, 2012 01:17 PM|LINK
Thank-you Catherine. When I get a chance I will read the articles you recommended and try to fix the PDF problem.
Rick
RDowdall
Member
361 Points
215 Posts
PDF upload to sql server 2005 failing
Nov 26, 2012 06:05 PM|LINK
I get the following error from Adobe Acrobat: There was an error opening this document. The file is damaged and could not be repaired.protected void UploadFile(object src, EventArgs e) { if (myFile.HasFile) { string fileName = Path.GetFileName(myFile.PostedFile.FileName); string fileExtension = Path.GetExtension(myFile.PostedFile.FileName); string documentType = string.Empty; switch (fileExtension) { case ".pdf": documentType = "application/pdf"; break; case ".xls": documentType = "application/vnd.ms-excel"; break; case ".xlsx": documentType = "application/vnd.ms-excel"; break; case ".docx": documentType = "application/vnd.ms-word"; break; case ".doc": documentType = "application/vnd.ms-word"; break; case "gif": documentType = "image/gif"; break; case "png": documentType = "image/png"; break; case "jpg": documentType = "image/jpg"; break; } int fileSize = myFile.PostedFile.ContentLength; byte[] documentBinary = new byte[fileSize]; myFile.PostedFile.InputStream.Read(documentBinary, 0, fileSize); Document doc = new Document(); doc.App_ID = Convert.ToInt32(ViewState["App_ID"]); doc.file_type = documentType; doc.file_size = fileSize; doc.file_name = fileName.ToString(); doc.uploaded_by = person; doc.uploaded_dt = DateTime.Now; doc.upload_doc = documentBinary; db.Documents.InsertOnSubmit(doc); try { db.SubmitChanges(); } catch (Exception ex) { ErrorHandler.LogException(ex.Message.ToString(), "Uploads.aspx", "UpLoadFile",Convert.ToInt32(doc.App_ID)); } } }ignatandrei
All-Star
134511 Points
21576 Posts
Moderator
MVP
Re: PDF upload to sql server 2005 failing
Nov 27, 2012 03:39 AM|LINK
compare the bytes of file downloaded with the bytes of file uploaded.
Catherine Sh...
All-Star
23373 Points
2490 Posts
Microsoft
Re: PDF upload to sql server 2005 failing
Nov 30, 2012 05:47 AM|LINK
Hi,
In order to resolve your issue, you need to check whether you have uploaded it correctly at first. Then I would like to suggest you to check the link below for the similar thread which provides some solutions for the same issue.
http://stackoverflow.com/questions/2440518/the-file-is-damaged-and-could-not-be-repaired
In addition, you can follow the article which describes about 4 ways to send a PDF file to the IE Client in ASP.NET .
Best wishes,
Feedback to us
Develop and promote your apps in Windows Store
RDowdall
Member
361 Points
215 Posts
Re: PDF upload to sql server 2005 failing
Nov 30, 2012 01:17 PM|LINK
Thank-you Catherine. When I get a chance I will read the articles you recommended and try to fix the PDF problem.
Rick