[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.






© 2024 - ErnesTech - Privacy
E-Commerce Return Policy