Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Saving file into database as binary

Saving file into database as binary

Scheduled Pinned Locked Moved ASP.NET
helpdatabase
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Deb_26
    wrote on last edited by
    #1

    Hi, I am saving pdf file into database reading from memroystream and it is saving properly, but when i try to retreive it it is throwing an error message "ile is damaged and could not be repaired". I am using itextsharp to generate the PDF, as this is urgent appreciate help. Thanks in advance.

    ashish

    M 1 Reply Last reply
    0
    • D Deb_26

      Hi, I am saving pdf file into database reading from memroystream and it is saving properly, but when i try to retreive it it is throwing an error message "ile is damaged and could not be repaired". I am using itextsharp to generate the PDF, as this is urgent appreciate help. Thanks in advance.

      ashish

      M Offline
      M Offline
      michaelschmitt
      wrote on last edited by
      #2

      Hi, without viewing at your code, it is difficult to decide where the problem is. I'm doing the same thing with itextsharp and a mysql db in some project. Here is how i basically do it:

      // Open ITextSharp Document and memstream
      Document myDocument = new Document(PageSize.A4);

      MemoryStream mstream = new MemoryStream();

      PdfWriter.GetInstance(myDocument, mstream );

      myDocument.Open();

      // Do some itextsharp stuff to myDocument

      // Close Document
      myDocument.Close();

      // Save stream to byte[] and save that byte[] to your db
      byte[] fileData = mstream.GetBuffer();

      // ...database-saving-logic....

      As you're posting this in the asp.net forum, i suppose you want to write this pdf to your reponse stream? (or to file?!) This is how i write it to response-stream in page-load of a download page (you have to replace mimeType, fileName, fileSize and fileData by your own variables/strings - with fileData being the byte[] and filesize the length of that byte[]):

      Response.ClearHeaders();
      Response.ClearContent();
      Response.ContentType = mimeType; // (guess it's "application/pdf")

      Response.AppendHeader("content-disposition", "attachment; filename=" +
      HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
      Response.AppendHeader("content-length", fileSize.ToString());
      Response.BinaryWrite(fileData);
      Response.End();

      Hope this helps. If you've problems writing/reading this data from/to a file, let me know.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups