[Solved]: Asp.Net Core Error 'byte[]' does not contain a definition for 'CopyToAsync' and no accessible extension method 'CopyToAsync' accepting a first argument of type 'byte[]' could be found (are you missing a using directive or an assembly reference?)

Problem: 'byte[]' does not contain a definition for 'CopyToAsync' and no accessible extension method 'CopyToAsync' accepting a first argument of type 'byte[]' could be found (are you missing a using directive or an assembly reference?)

Solution: Navigate to the class model (DTO) and change the property type from "byte[]" or any property you where using to "IFormFile" e.g.

public IFormFile NameOfYourProperty {get; set;}

 

[Note] This topic can be complicated, sometimes Asp.Net Core would require you to create a new DTO class with a property IFormFile then in your Controller Function parameter Inject  in the Newly created DTO Class class that has an IFormFile property (expect a newly created DTO onPost model). In the same function then, read the bites from the IFormFile and then proceed from there.

 

[Note] This is due to complication of IFormFile in Asp.Net Core;
          -  create similer dto Model with IFormFile property
            then bind with Original dto that has byte[] property to be able to store
            bytes file.

[NB] If you resolve the above error and then you find out that the actual file is not getting posted to the Server, make sure that the Form tag in Asp.Net Core MVC Razor page has an enctype="multipart/form-data" method="post" otherwise you will spend days troubleshooting.


Edit this Article
asp.net Core 3.1 asp.net mvc asp.net core errors asp.net web forms asp.net core tutorial Technology published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy