I am attempting to upload an image using MVC 6
; however, I am not able to find the class HttpPostedFileBase
. I have checked the GitHub
and did not have any luck. Does anyone know the correct way to upload a file in MVC6
?
The HttpPostedFileBase class is an abstract class that contains the same members as the HttpPostedFile class. The HttpPostedFileBase class lets you create derived classes that are like the HttpPostedFile class, but that you can customize and that work outside the ASP.NET pipeline.
What is IFormFile. ASP.NET Core has introduced an IFormFile interface that represents transmitted files in an HTTP request. The interface gives us access to metadata like ContentDisposition, ContentType, Length, FileName, and more. IFormFile also provides some methods used to store files.
MVC 6 used another mechanism to upload files. You can get more examples on GitHub or other sources. Just use IFormFile
as a parameter of your action or a collection of files or IFormFileCollection
if you want upload few files in the same time:
public async Task<IActionResult> UploadSingle(IFormFile file) { FileDetails fileDetails; using (var reader = new StreamReader(file.OpenReadStream())) { var fileContent = reader.ReadToEnd(); var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition); var fileName = parsedContentDisposition.FileName; } ... } [HttpPost] public async Task<IActionResult> UploadMultiple(ICollection<IFormFile> files) { var uploads = Path.Combine(_environment.WebRootPath,"uploads"); foreach(var file in files) { if(file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); await file.SaveAsAsync(Path.Combine(uploads,fileName)); } ... } }
You can see current contract of IFormFile
in asp.net sources. See also ContentDispositionHeaderValue
for additional file info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With