E' molto importante ricordare nel "form" la proprietà enctype="multipart/form-data" e nell'oggetto "input" la proprietà multiple="multiple"
Nell'esempio è presente la parte client/html (Default.aspx), il codice associato (Default.aspx.cs) e le immagini d'aiuto.
Default.aspx
<asp:%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>asp:
<asp:html xmlns="http://www.w3.org/1999/xhtml">asp:
<asp:head runat="server">asp:
<asp:title>asp:<asp:/title>asp:
<asp:/head>asp:
<asp:body>asp:
<asp:form id="form1" runat="server" enctype="multipart/form-data" method="post" >asp:
<asp:table cellpadding="2" cellspacing="1" width="740px">asp:
<asp:tr>asp:
<asp:td width="120px">asp:
<asp:input type="file" id="File1" multiple="multiple" name="File1" runat="server" />asp:
<asp:/td>asp:
<asp:/tr>asp:
<asp:tr>asp:
<asp:td align="left">asp:
<asp:asp:Button id="Manual" Text="Upload" runat="server" OnClick="Upload_Click" />asp:
<asp:/td>asp:
<asp:/tr>asp:
<asp:tr>asp:
<asp:td colspan="4">asp:
<asp:asp:ListBox ID="list_message" name="list_message" runat="server" width="600px" Height="200px">asp:<asp:/asp:ListBox>asp:
<asp:/td>asp:
<asp:/tr>asp:
<asp:/table>asp:
<asp:/form>asp:
<asp:/body>asp:
<asp:/html>asp:
Default.aspx.cspublic partial class _Default : System.Web.UI.Page
{
string pathFileUploaded = "c:\\temp\\";
protected void Page_Load(object sender, EventArgs e)
{}
protected void Upload_Click(object sender, System.EventArgs e)
{
if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength >asp: 0))
{
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i <asp: uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
try
{
if (userPostedFile.ContentLength >asp: 0)
{
string fn = System.IO.Path.GetFileName(userPostedFile.FileName);
try
{
userPostedFile.SaveAs(pathFileUploaded + fn);
list_message.Items.Add(userPostedFile.FileName + " inviato.");
}
catch (Exception ex)
{
list_message.Items.Add("Errore: " + ex.Message);
}
}
}
catch (Exception Ex)
{
list_message.Items.Add("Errore: " + Ex.Message);
}
}
}
else
{
list_message.Items.Add("Selezionare almeno 1 file.");
}
}
}