Answered by:
How to check for null in var object

Question
-
User248267340 posted
I have a var object as follows:
var files = HttpContext.Request.Form.Files;
In my form, I'm allowing the user to state the local paths of 10 pictures I want uploaded to the form, where
the first 3 paths are set, and the remaining 7 are still null.
This doesn't work:
if (!string.IsNullOrWhiteSpace(files[4].FileName)) files[4].CopyTo(filestream);
How can I get this right?
Wednesday, August 7, 2019 6:54 PM
Answers
-
User475983607 posted
I do not understand the problem. It seems you are trying to index into a array and perhaps receiving a index out or rage exception? It is always best to share the actual exception.
I recommend reading the ASP.NET Core file upload documentation as it illustrates, with source code, how to read multiple files using a foreach loop.
https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.2
[HttpPost("UploadFiles")] public async Task<IActionResult> Post(List<IFormFile> files) { long size = files.Sum(f => f.Length); // full path to file in temp location var filePath = Path.GetTempFileName(); foreach (var formFile in files) { if (formFile.Length > 0) { using (var stream = new FileStream(filePath, FileMode.Create)) { await formFile.CopyToAsync(stream); } } } // process uploaded files // Don't rely on or trust the FileName property without validation. return Ok(new { count = files.Count, size, filePath}); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 7, 2019 7:01 PM
All replies
-
User475983607 posted
I do not understand the problem. It seems you are trying to index into a array and perhaps receiving a index out or rage exception? It is always best to share the actual exception.
I recommend reading the ASP.NET Core file upload documentation as it illustrates, with source code, how to read multiple files using a foreach loop.
https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.2
[HttpPost("UploadFiles")] public async Task<IActionResult> Post(List<IFormFile> files) { long size = files.Sum(f => f.Length); // full path to file in temp location var filePath = Path.GetTempFileName(); foreach (var formFile in files) { if (formFile.Length > 0) { using (var stream = new FileStream(filePath, FileMode.Create)) { await formFile.CopyToAsync(stream); } } } // process uploaded files // Don't rely on or trust the FileName property without validation. return Ok(new { count = files.Count, size, filePath}); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 7, 2019 7:01 PM -
User248267340 posted
Thanks so much - the snippet answered all my questions.
I appreciate your insight - I admit that I'm new at this, and am learning from an online website. When I have questions,
it's impossible to get hold of the instructor.
Sorry that I didn't include the error message. But this is great! I'll follow the link as well.
Again, thanks a million!
Corey
Wednesday, August 7, 2019 8:58 PM