asp.net 中一次上傳多個檔案
看到一篇老外的文章,說在asp.net 中,如何先讓使用者把要上傳的檔案都選好了,然後一次上傳,
今小結如下
首先在頁面加一個上傳檔案控制元件,一個“attach"按鈕,一個listbox,用來存放等待上傳的檔名,
一個"UPLOAD"按鈕,一個”刪除按鈕
<form id="Form1" method="post" runat="server">
<INPUT id="FileUpload" style="Z-INDEX: 101; LEFT: 83px; WIDTH: 489px; POSITION: absolute; TOP: 67px; HEIGHT: 22px"
type="file" size="62" runat="server">
<asp:button id="btnAttach" style="Z-INDEX: 102; LEFT: 591px; POSITION: absolute; TOP: 66px"
runat="server" Text="Attach"></asp:button><asp:listbox id="ListBox1" style="Z-INDEX: 103; LEFT: 84px; POSITION: absolute; TOP: 104px" runat="server"
Width="565px" Height="93px"></asp:listbox><asp:button id="btnUpload" style="Z-INDEX: 104; LEFT: 91px; POSITION: absolute; TOP: 198px"
runat="server" Text="Upload"></asp:button><asp:button id="btnDelete" style="Z-INDEX: 105; LEFT: 684px; POSITION: absolute; TOP: 131px"
runat="server" Text="Delete" Width="58px"></asp:button>
<asp:Label id="Label1" style="Z-INDEX: 106; LEFT: 166px; POSITION: absolute; TOP: 199px" runat="server"
Width="476px" ForeColor="Red"></asp:Label></form>
之後,在"attach"按鈕中程式碼如下:
private void btnAttach_Click(object sender, System.EventArgs e)
{
// Save the attached file to fileName variable
string fileName = FileUpload.PostedFile.FileName;
// If the counter is null then create one with default value equal to 0
if(ViewState["i"] == null)
{
ViewState["i"]= 0;
}
// Check if a file is selected
if(fileName != null || fileName != string.Empty)
{
// Add it to the collection
ListBox1.Items.Add(FileUpload.PostedFile.FileName);
// Save an index for each selected file
int i = Convert.ToInt32(ViewState["i"]);
// Save the fileupload control into a different session
Session["myupload" + i] = FileUpload;
// Increment the counter
i++;
// Set the ViewSate to the latest counter value.
ViewState["i"] = i;
}
}
很明顯,其實是用viewstate來存放使用者上傳的實際檔名,這需要使用者在選擇檔案後用
"attach"按鈕來將檔案新增到那個listbox框中去
之後是“upload"的程式碼,
private void btnUpload_Click(object sender, System.EventArgs e)
{
int sessionCount = Session.Count;
///int sessionCount = Convert.ToInt32(ViewState["i"]);
for( int i =sessionCount-1;i>=0;i--)
{
if(sessionCount <= 3)
{
HtmlInputFile hif = (HtmlInputFile)Session["myupload" + i];
if(hif.PostedFile.ContentLength <= 500000)
{
string storePath = Server.MapPath("~") + "/MultipleUpload";
if(!Directory.Exists(storePath))
Directory.CreateDirectory(storePath);
hif.PostedFile.SaveAs(storePath + "/" + Path.GetFileName(hif.PostedFile.FileName));
Label1.Text = "Your Files are uploaded successfully";
ListBox1.Items.Clear();
}
else
Label1.Text = "An error occured";
}
else
Label1.Text = "You have exceeded the maximum number of files to be uploaded (3)";
}
Session.RemoveAll();
}
實際上是將所有的要上傳的檔案從session裡取回來,然後每一個逐一上傳
最後是把要上傳的檔案從listbox裡刪除的程式碼,在session裡remove掉
private void btnDelete_Click(object sender, System.EventArgs e)
{
if(ListBox1.SelectedIndex > -1)
{
int uploadedFileIndex = ListBox1.SelectedIndex;
Session.Remove("myupload" + uploadedFileIndex);
ListBox1.Items.Remove(ListBox1.SelectedValue);
}
}
總結一下,用在上傳檔案不多的情況下比較好,因為要用session