I am trying to upload multiple images from a form. And then grab the images using WebImage.GetImageFromRequest(). This works fine with a single upload field, but I'm not sure how to use this to get multiple files.
My HTML form:
<input id="uploadBtn1" type="file" name="Image1" accept=".jpg,.png" class="upload"/>
<input id="uploadBtn2" type="file" name="Image2" accept=".jpg,.png" class="upload"/>
<input id="uploadBtn3" type="file" name="Image3" accept=".jpg,.png" class="upload"/>
My C# Code
if (IsPost)
{
photo1 = WebImage.GetImageFromRequest("image1");
newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo1.FileName);
imagePath = @"branding\" + newFileName;
photo1.Save(@"~\" + imagePath);
photo2 = WebImage.GetImageFromRequest("image2");
newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo2.FileName);
imagePath = @"branding\" + newFileName;
photo2.Save(@"~\" + imagePath);
photo3 = WebImage.GetImageFromRequest("image3");
newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo3.FileName);
imagePath = @"branding\" + newFileName;
photo3.Save(@"~\" + imagePath);
}
This throws an error: System.NullReferenceException: Object reference not set to an instance of an object. Line 101: newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo3.FileName);
I think that I'm using the incorrect syntax for: photo1 = WebImage.GetImageFromRequest("image1");
How do I specify this to get the the "image1" upload field? And "image2", "image3" etc?
Did you include
into your form?
Example:
Here is an example how to iterate through multiple files: