Asp.net Webforms - 如何使用二进制读取器读取文件

时间:2016-10-18 07:40:31

标签: c# asp.net image-upload

我有一个文件,我在完成一些图像大小调整后上传到临时文件夹,如下所示:

/uploads/temp/myfile.jpg

如果我所拥有的是如上所示的文件路径,如何将此文件读入二进制阅读器,如下所示,在下面的示例中,我收到错误:

Error in saving fileSystem.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\\uploads\\temp\\myfile.jpg'

 var fs = new FileStream('/uploads/temp/myfile.jpg', FileMode.Open);
 using (var reader = new BinaryReader(fs))
 {
      image.ImageContent = reader.ReadBytes((int)fs.Length);
 }

1 个答案:

答案 0 :(得分:0)

通过执行以下操作来管理修复:

 var fs = new FileStream(HostingEnvironment.MapPath("~/") + "/uploads/temp/myfile.jpg", FileMode.Open);