【问题标题】:Upload file image in ASP.NET Core在 ASP.NET Core 中上传文件图像
【发布时间】:2020-04-01 04:28:35
【问题描述】:

将文件保存到 Wwwroot 后,我​​遇到了 1 个问题。但是不知道是什么原因。 This is the error I received after the add successfully this image file to Wwwroot. It asks to close 1 app that I don't know.

但是当我打开文件夹 images 并双击该图像或重建项目时,它恢复正常 Here's the photo in the folder

And here is the result

我的代码:

  1. Code Save Image
  2. Controller

【问题讨论】:

  • 你真的应该发布你的代码。上传文件时听起来好像没有正确关闭流。
  • 好的。有更新:)
  • @coderpro 我同意,我认为他应该关闭流。

标签: asp.net image asp.net-core file-upload asp.net-core-3.0


【解决方案1】:

在尝试签入挂起的更改(冲突)时会发生这种情况,因为您的项目位于 TFS 上。

您必须签入图像文件夹或从项目中排除图像

【讨论】:

  • 是因为我用的是GIT吗??
【解决方案2】:

这是我最近为相同情况输入的一些代码。编辑,只为你:)

using (var ms = new MemoryStream()) 
{
    await file.CopyToAsync(ms);

    using (var writer = System.IO.File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
    {
        writer.Write(ms.ToArray());
    }
}

请注意,如果文件已存在,此代码也会打开该文件,如果不存在则创建。

此外,通过使用using,我们正在清理自己,内存明智。

【讨论】:

  • 好吧。我试过了,效果很好。但是有一个问题是我不能上传更大的文件 384kb :)
猜你喜欢
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 2018-04-21
  • 2018-09-04
  • 1970-01-01
  • 2018-07-29
相关资源
最近更新 更多