使用asp.net中的FileUpload控件一次选择多个文件
本文关键字:一次 选择 文件 net asp 中的 FileUpload 控件 使用 | 更新日期: 2023-09-27 18:11:27
这是我的代码
protected void Page_Load(object sender, EventArgs e)
{
tbusrcompdate.Attributes.Add("readonly", "readonly");
if (!IsPostBack)
{
//using session state
if (Session["userid"] == null)
{
Session["userid"] = "Compliance_Tracker";
lblsessionID.Text = Session["userid"].ToString();
}
else
{
lblsessionID.Text = Session["userid"].ToString();
}
//populating ddlTaskId
string query = "select * from Compliance_Tracker.dbo.tasklistManager where STATUS='1';";
string columnname = "TASK ID";
string datavaluefield = "TASK ID";
obj7.PopulateCombo(ddlTaskID, query, columnname, datavaluefield);
//default values in labels
string query1 = "select top 1 [DESC] from Compliance_Tracker.dbo.tasklistManager where STATUS = '1';";
lblDescOutput.Text = obj7.ExecuteScalar(query1).ToString();
string query2 = "select top 1 FREQUENCY from Compliance_Tracker.dbo.tasklistManager where STATUS = '1';";
lblFrequencyOutput.Text = obj7.ExecuteScalar(query2).ToString();
}
}
protected void ddlTaskID_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedtext = ddlTaskID.SelectedValue.ToString();
string query = "select [DESC] from Compliance_Tracker.dbo.tasklistManager where Compliance_Tracker.dbo.tasklistManager.[TASK ID] ='" + selectedtext + "';";
lblDescOutput.Text = obj7.ExecuteScalar(query).ToString();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (fuDocpath.HasFiles)
{
try
{
DateTime now = DateTime.Now;
lbldateStamp.Text = now.ToString("mm_dd_yyyy_hh_mm_ss");
string basicPath = Server.MapPath("~/Uploaded_Files/");
string foldername = lblsessionID.Text + "_" + lbldateStamp.Text;
string folderpath = (basicPath + foldername + "/");
Directory.CreateDirectory(folderpath);
foreach (HttpPostedFile file in fuDocpath.PostedFiles)
{
string filename = Path.GetFileName(fuDocpath.FileName);
string folderpath1 = folderpath + "/";
fuDocpath.SaveAs(folderpath1 + filename);
lblName.Text = lblName.Text+"|" + filename;
lblerror.Text = string.Empty;
}
}
catch (Exception ex)
{
lblerror.Text = "File couldn't be uploaded." + ex.Message;
lblName.Text = string.Empty;
}
}
}
现在,使用上面的代码,我只能做一个选择,即使我可以上传多个文件。所以我需要知道,如果有一种方法来选择多个文件在一次使用文件上传工具或我将不得不使用任何其他工具?这是我的aspx代码
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server">
<form id="Form1" runat="server" style="border-style: none; border-width: inherit; border-color: #008000; background-color:#33CC33; height:588px; width:669px; background-image: url('new.jpg'); background-repeat: no-repeat;" method="post" enctype="multipart/form-data">
<h1 style="height: 34px">
TRANSACTION MANAGER TABLE
</h1>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Label ID="lblsessionID" runat="server" Text=""></asp:Label>
<asp:Label ID="lbldateStamp" runat="server" Text=""></asp:Label>
<table id="table1" style="border-style: none; height:188px; width:549px; margin-left:30px; border-collapse: collapse; margin-top: 0px;">
<tr>
<td style="width: 137px; height:30px;"><asp:Label ID="lblTaskID" runat="server" Text="TASK ID" Width="70px"></asp:Label></td>
<td style="height:30px"><asp:DropDownList ID="ddlTaskID" runat="server" Height="20px" style="margin-left: 50px" Width="126px" OnSelectedIndexChanged="ddlTaskID_SelectedIndexChanged" AutoPostBack="true" ></asp:DropDownList></td>
</tr>
<tr>
<td style="width:137px; height:30px;"><asp:Label ID="lblDesc" runat="server" Text="DESC" Width="70px"></asp:Label></td>
<td style ="height:30px"><asp:Label ID="lblDescOutput" runat="server" style="margin-left:50px" Width="126px" Height="20px"></asp:Label></td>
</tr>
<tr>
<td style="width: 137px; height:30px;"><asp:Label ID="lblFrequency" runat="server" Text="FREQUENCY" Width="132px"></asp:Label></td>
<td style="height:30px"><asp:Label ID="lblFrequencyOutput" runat="server" style="margin-left:50px" Width="126px" Height="20px"></asp:Label></td>
</tr>
<tr>
<td style="width: 137px; height:30px;"><asp:Label ID="lblDocpath" runat="server" Text="DOC PATH" Width="107px"></asp:Label></td>
<td style="height:30px">
<asp:FileUpload ID="fuDocpath" runat="server" AllowMultiple="true" />
<asp:Button ID="Button1" runat="server" Text ="Upload File" OnClick="Button1_Click" />
<asp:Label ID="lblerror" runat="server" Text="" style="margin-left: 50px"></asp:Label>
<asp:Label ID="lblName" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td style="width: 137px; height:30px;"><asp:Label ID="lblusrcompdate" runat="server" Text="USER COMPLETE DATE" Width="147px"></asp:Label></td>
<td style="height:30px"><asp:TextBox ID="tbusrcompdate" runat="server"></asp:TextBox>
<asp:ImageButton ID ="imgbtncalender" runat="server" ImageUrl="~/home-calendar-button.jpg" Height="17px" style="margin-left: 16px" Width="16px" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="tbusrcompdate" Format="dd/MM/yyyy" PopupButtonID="imgbtncalender"></asp:CalendarExtender>
</td>
</tr>
</table><br />
<asp:Button ID="btnAdd" runat="server" Text="ADD" Height="27px" Width="80px" style="margin-left:235px" OnClick="btnAdd_Click"/>
</form>
实际问题是IE,更新后我现在可以选择多个文件。另一个问题是,我的代码实际上只有一个文件被选中,因为我没有正确地获取文件名。实际上,在我声明文件名字符串
的部分foreach (HttpPostedFile files in fuDocpath.PostedFiles)
{
string filename = Path.GetFileName(fuDocpath.FileName);
string folderpath1 = folderpath + "/";
fuDocpath.SaveAs(folderpath1 + filename);
lblName.Text = lblName.Text+"|" + filename;
lblerror.Text = string.Empty;
}
现在,这里的主要问题是我没有得到正确的文件名。所以,错误是
string filename = Path.GetFileName(files.Filename)
代替fuDocpath.filename。愚蠢的错误!!
这不是您特定代码的确切答案。但这是上传多个文件的另一种选择。
对于上传多个文件,我不做所有这些,但是对于这个
有一个叫做asp:AjaxFileUpload
控件的东西<asp:AjaxFileUpload ID="AjaxFileUpload1" ToolTip="Uplaod File" OnUploadComplete="AjaxFileUpload1_UploadComplete" runat="server" />
代码隐藏:
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
try
{
string str = System.IO.Path.GetFileName(e.FileName);
string fullpath = Path.GetFullPath(str);
string str1 = "Your insert Query";
con.Open();
cmd = new SqlCommand(str1, con);
// your command parameters to add
cmd.Parameters.AddWithValue("@Image_Caption", str);
cmd.Parameters.AddWithValue("@Image_Path", "Images/" + str);
cmd.Parameters.AddWithValue("@Image_Name", str);
cmd.ExecuteNonQuery();
con.Close();
string filePath = Server.MapPath("~/Images/") + System.IO.Path.GetFileName(e.FileName);
AjaxFileUpload1.SaveAs(filePath);
}
catch (Exception eep)
{
throw;
}
}