【问题标题】:Asp.net Webforms - How to Read a file Using Binary ReaderAsp.net Webforms - 如何使用二进制阅读器读取文件
【发布时间】:2016-10-18 07:40:31
【问题描述】:

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

/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);
 }

【问题讨论】:

  • 您可以将图像存储在文件夹中,并将图像地址存储在数据库中。
  • @Ali 我已经知道我该怎么做。我的问题是关于存储到数据库。
  • 您是否询问如何从临时文件夹加载图像并将其存储到数据库中?如果是这样,您使用的是什么类型的数据库以及如何在数据库中存储其他数据?
  • @user2154065 存储到 SQL Server 数据库中。我只需要知道如何从临时文件夹中读取文件并保存到数据库中。
  • 使用 FileStream 或 BinaryReader 打开文件读取然后使用 SQL Server 连接,命令对象写入数据库。哦!刚刚看到您正在使用我不熟悉的东西。请忽略我的评论。

标签: c# asp.net image-upload


【解决方案1】:

通过以下方式设法修复:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多