Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 6 HttpPostedFileBase?

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?

like image 224
Steven Mayer Avatar asked Apr 23 '15 23:04

Steven Mayer


People also ask

What is HttpPostedFileBase in MVC?

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 c#?

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.


1 Answers

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.

like image 156
Vadim Martynov Avatar answered Oct 08 '22 11:10

Vadim Martynov