2012-06-01 74 views
1

我已經建立了一個asp.net多文件上傳,它的工作原理,但沒有以我期望的方式。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'的成員。

EDIT 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" /> 

回答

0

Request.Files("gallery")由於其屬性不是方法而無效。

您可以通過請求PostedFile值然後保存到文件系統中,從Gallery輸入中獲取張貼的文件。

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

顯然你可以使文件名稱任意你想要的。

+0

嗨 - 我使用它來上傳多個圖片 - 我添加了一個顯示我正在使用的額外代碼的編輯。 –

+0

目前我很困惑這一切 - 我試圖實現的是從其他單個文件上傳的頁面上傳多個文件 - 看起來很複雜!感謝您的幫助:) –

+0

查看我的更新 - 它是從頁面上傳所有圖片,而不是從「gallery」發佈的圖片,如果這樣做有道理的話? –