Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation error trying to save image .NET Core - Argument type 'System.IO.FileStream' is not assignable to parameter type 'System.IO.Stream'

Tags:

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

I'm getting a compilation error trying to save image .NET Core -

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

Why is that? Should I use another streamwriter? I'm not sure if there's a package missing?

.csproj references:

  <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 metadata top:

#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
like image 232
Lindeberg Avatar asked Jun 21 '17 20:06

Lindeberg


People also ask

What is a FileStream in C#?

The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare.

What is the use of FileStream?

Use the FileStream class to read from, write to, open, and close files on a file system, and to manipulate other file-related operating system handles, including pipes, standard input, and standard output.


1 Answers

For my case, i had the similar problem. By updating Resharper to the version 2017.2 EAP 3, it solved my problem. If you're also using Resharper, try to update it :

Download & install 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");
            }
like image 135
Volkan Avatar answered Sep 30 '22 14:09

Volkan