gpt4 book ai didi

c# - 尝试保存图像 .NET Core 时出现编译错误 - 参数类型 'System.IO.FileStream' 不可分配给参数类型 'System.IO.Stream'

转载 作者:太空狗 更新时间:2023-10-30 01:14:16 24 4
gpt4 key购买 nike

Image

  [HttpPost]
public async Task<IActionResult> Upload(string memberNumber, IFormFile file)
{
var uploadsFolderPath = Path.Combine(_host.WebRootPath, "uploads");

var fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
var filePath = Path.Combine(uploadsFolderPath, fileName);

using (var stream = new FileStream(filePath, FileMode.Create))
{
// Here's the compilation error
await file.CopyToAsync(stream);
}

var photo = new Photo(fileName);
var member = _unitOfWork.Members.GetByMemberNumber(memberNumber);
member.Photo = photo;
_unitOfWork.Complete();

return Ok(_mapper.Map<Photo, PhotoResource>(photo));
}

我在尝试保存图像 .NET Core 时遇到编译错误 -

Argument type 'System.IO.FileStream' is not assignable to parameter type 'System.IO.Stream' 

这是为什么呢?我应该使用另一个 streamwriter 吗?我不确定是否缺少包裹?

.csproj 引用:

  <ItemGroup>
<PackageReference Include="AutoMapper" Version="6.1.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="2.0.1" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />

文件流元数据顶部:

#region Assembly System.IO.FileSystem, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a//C:\Users\Leon\.nuget\packages\system.io.filesystem\4.3.0\ref\netstandard1.3\System.IO.FileSystem.dll#endregion

最佳答案

就我而言,我遇到了类似的问题。通过将 Resharper 更新到版本 2017.2 EAP 3,它解决了我的问题。如果您也在使用 Resharper,请尝试更新它:

下载并安装 2017.2 EAP 3 https://www.jetbrains.com/resharper/eap/

            [HttpPost]
public async Task<IActionResult> UploadFile(IFormFile file)
{
string filePath = "UploadedFiles/" + Guid.NewGuid() + Path.GetExtension(file.FileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
return Json("file uploaded successfully");
}

关于c# - 尝试保存图像 .NET Core 时出现编译错误 - 参数类型 'System.IO.FileStream' 不可分配给参数类型 'System.IO.Stream',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44685370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com