在 ASP.NET Core 中上传文件图像

Upload file image in ASP.NET Core

将文件保存到 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.

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

And here is the result

我的代码:

  1. Code Save Image
  2. Controller

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

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

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

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,我们正在清理自己的记忆。