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();
It could be a permissions issue... The account ASP.net runs under has to have write privileges to the target directory.
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?
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
来源:https://stackoverflow.com/questions/5017980/save-file-from-byte-array-on-servermap-path