2017-06-21 9 views
4

Image編譯錯誤試圖保存圖像.NET的核心 - 參數類型「System.IO.FileStream」是不能分配給參數類型「System.IO.Stream」

[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" /> 

的FileStream元頂:

#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 
+2

向我們展示你的.csproj文件的參考部分。此外,如果您右鍵單擊並查看新文件流中的元數據(「請致電並向我們顯示新窗口頂部的註釋部分中的文本 –

+0

@ScottChamberlain我已更新該問題,謝謝! – Lindeberg

+0

你使用Resharper?你能告訴我們它的版本嗎? – Volkan

回答

4

對於我而言,我有類似的問題。通過將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"); 
      } 
+0

謝謝,你釘了它! 我不會嘗試EAP,但暫停Resharper消除了錯誤。 – Lindeberg

相關問題