1

I Want to save a PDF file from a byte array and want to save that file on my server map path location.

Below is my code snippet. It's giving no errors nor saving the file. You are welcome to correct my syntax if it is wrong or help me by referring other code snippets.

byte[] data = (byte[])listDataset.Tables[0].Rows[0][0];

System.IO.FileStream file = System.IO.File.Create(Server.MapPath(".\\TmpImages\\"+hfFileName+".pdf "));

file.Write(data, 0, data.Length);
file.Close();
3
  • 3
    Not an answer, but you could simplify this by using File.WriteAllBytes: msdn.microsoft.com/en-us/library/… Commented Feb 16, 2011 at 15:00
  • 2
    What doesn't work exactly? Are you getting an error? Commented Feb 16, 2011 at 15:01
  • AArsian: Like @Thomas asks: Full Error Message and stack trace please. Commented Feb 16, 2011 at 15:19

3 Answers 3

2

Another "not answer", but maybe helpful to rule some stuff out. I tried

  byte[] data = new byte[] { 12, 14, 63, 45, 3 };

  System.IO.FileStream file = System.IO.File.Create(HttpContext.Current.Server.MapPath(".\\imageLibrary\\test.pdf "));

  file.Write(data, 0, data.Length);
  file.Close();

and it worked fine (test.pdf was created). I had thought that the space at the end of your file path could be causing problems but that's not it.

Are you sure you haven't enclosed this block in a try{}catch{} block that might be swallowing a path or permissions error? Have you tried setting a breakpoint on the file.Close() line to make sure it gets that far?

Sign up to request clarification or add additional context in comments.

Comments

1

It could be a permissions issue... The account ASP.net runs under has to have write privileges to the target directory.

Comments

0
Server.MapPath(".\\TmpImages\\"+hfFileName+".pdf ")

Are you sure the path is what you expect it to be? If you're sure you're not getting an exception (i.e. permissions), then I would recommend debugging and seeing what value the call to Server.MapPath returns.

It's possible that the file is being written out, but to a different location than you expected.

"If [the argument to MapPath] doesn't start with a slash, the MapPath method returns a path relative to the directory of the .asp file being processed." http://msdn.microsoft.com/en-us/library/ms524632%28v=vs.90%29.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.