I have one post method in which one file upload control is there. To get that uploaded file in controller, I am getting error like below :
Error CS1061 'HttpRequest' does not contain a definition for 'Files' and no extension method 'Files' accepting a first argument of type 'HttpRequest' could be found (are you missing a using directive or an assembly reference?)
My code is like below :
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<IActionResult> TeamUserDetail(TeamUsers model)
{
var file = Request.Files[0];
return View();
}
At Request.Files[0] it is giving error shown above. MVC6 is used in project.
Please guide me for it. Am I missing any reference to add?
Thank You
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:
public FileDetails UploadSingle(IFormFile file)
{
FileDetails fileDetails;
using (var reader = new StreamReader(file.OpenReadStream()))
{
var fileContent = reader.ReadToEnd();
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
fileDetails = new FileDetails
{
Filename = parsedContentDisposition.FileName,
Content = fileContent
};
}
return fileDetails;
}
[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));
}
}
return View();
}
You can see current contract of IFormFile in asp.net sources.
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