Save file from byte array on servermap path

半城伤御伤魂 提交于 2020-01-14 03:39:07

问题


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();

回答1:


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




回答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?




回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!