1

我已经设置了一个 asp.net 多文件上传,它可以工作,但不是我期望的那样。

在我的页面上,我有 2 个像这样的图片上传器

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

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

我的问题是当我上传它时使用此代码

Dim hfc As HttpFileCollection = Request.Files

要获取所有已发布的文件,但我只想要gallery此特定方法的图像。

我有不同的方法来上传我的其他图片。

我尝试将其更改为以下

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

但我得到一个错误

“System.Web.HttpPostedFile”类型的值无法转换为“System.Web.HttpFileCollection”。

有什么想法可以做到这一点吗?

谢谢

编辑

这是我正在使用的完整代码

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

当我使用下面答案中的代码时,我收到一条错误消息

“Count”不是“System.Web.HttpPostedFile”的成员。

编辑 2

这适用于上传我的所有图片

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

但它会上传每张图片 - 我只想让它上传从

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

而不是这个

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

1 回答 1

0

Request.Files("gallery")无效,因为它是属性而不是方法。

您可以通过请求 PostedFile 值从 Gallery 输入中获取 Posted 文件,然后像您所做的那样保存到文件系统。

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

您显然可以将文件名设置为任何您想要的名称。

于 2012-06-02T18:06:51.037 回答