我正在尝试使用控件上传多个图像,FileUpload
但无法做到
我试过了:
<asp:FileUpload ID="fuImage" AllowMultiple="true" runat="server"/>
这也是:
<asp:FileUpload ID="fuImage" multiple="multiple" runat="server"/>
但是当我在谷歌上搜索几个小时时,我发现这仅适用于 4.5 版而不适用于 4.0
protected void btnAdd_Click(object sender, EventArgs e)
{
if (fuimage.HasFile)
{
string fname = fuimage.FileName;
string path = Server.MapPath("~/EventPics/");
string fext = Path.GetExtension(fname);
fext = fext.ToLower();
string link = "~/EventPics/" + fname;
if (fext == ".jpg" || fext == ".png" || fext == ".gif" || fext == ".bmp")
{
fuimage.SaveAs(path + fname);
con = new SqlConnection(ConfigurationManager.ConnectionStrings["WebAAERT_DBConnectionString"].ConnectionString);
SqlCommand cmd;
//create command
cmd = new SqlCommand("EventMasterInsert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ImagePath", link);
cmd.Parameters.AddWithValue("@EventTitle", txtEventTitle.Text);
cmd.Parameters.AddWithValue("@EventDate", txtEventDate.Text);
cmd.Parameters.AddWithValue("@EventPlace", txtPlace.Text);
cmd.Parameters.AddWithValue("@ShortDescription", txtShort.Text);
cmd.Parameters.AddWithValue("@Description", txtDesc.Text);
cmd.Parameters.AddWithValue("@EventTime", txtTime.Text);
//open connection
cmd.Connection = con;
con.Open();
//execute command
int rowcount = cmd.ExecuteNonQuery();
if (rowcount > 0)
{
Response.Write("<script>alert('Event Added');</script>");
txtDesc.Text = "";
txtEventDate.Text = "";
txtEventTitle.Text = "";
txtPlace.Text = "";
txtShort.Text = "";
txtTime.Text = "";
}
}
}
我试过了
foreach(item in fuImage.postedFile)
{
}
但它不工作..不知道如何以最简单的方式做到这一点..