asp.netvb.netimage-uploadingmulti-upload

Asp.Net multiple file upload


I've set up an asp.net multiple file upload and it works but not in the way I would expect it to.

On my page I've got 2 image uploaders like so

<input type="file" id="gallery" class="multi" accept="jpg" runat="server" />

<input type="file" id="pic1" accept="jpg" runat="server" />

My problem is when I upload it uses this code

Dim hfc As HttpFileCollection = Request.Files

To get all the files which were posted but I only want gallery images for this specific method.

I have a different method to upload my other image.

I've tried changing it to the following

Dim hfc As HttpFileCollection = Request.Files("gallery")

But I get an error

Value of type 'System.Web.HttpPostedFile' cannot be converted to 'System.Web.HttpFileCollection'.

Any ideas how I can accomplish this?

Thanks

EDIT

Here's the full piece of code I'm working with

Dim hfc As HttpFileCollection = Request.Files("gallery")
For i As Integer = 0 To hfc.Count - 1
Dim hpf As HttpPostedFile = hfc(i)
If hpf.ContentLength > 0 Then
    hpf.SaveAs(Server.MapPath("/images/" & i & ".jpg"))
End If
Next i

When I use the code from the answer below I get an error saying

'Count' is not a member of 'System.Web.HttpPostedFile'.

EDIT 2

This works with uploading all my images

Dim hfc As HttpFileCollection = Request.Files
For i As Integer = 0 To hfc.Count - 1
Dim hpf As HttpPostedFile = hfc(i)
If hpf.ContentLength > 0 Then
hpf.SaveAs(Server.MapPath("/images/" & i & ".jpg"))
End If
Next i

But it uploads every image - I just want it to upload files posted from

<input type="file" id="gallery" class="multi" accept="jpg" runat="server" />

and not this one as well

<input type="file" id="pic1" accept="jpg" runat="server" />

Solution

  • Request.Files("gallery") is invalid as its a property not a method.

    You can get the Posted file from Gallery input by requesting the PostedFile value then save to the file system as you have been doing.

    Dim hfc As System.Web.HttpPostedFile = gallery.PostedFile
    If hpf.ContentLength > 0 Then
       hpf.SaveAs(Server.MapPath("/images/GalleryImage.jpg"))
    End If
    

    You can obviously make the file name anything you wish.