C# 如何在asp.net c的framework 4.0中使用FileUpload控件选择多个图像#
我正在尝试使用C# 如何在asp.net c的framework 4.0中使用FileUpload控件选择多个图像#,c#,asp.net,.net,file-upload,multiple-file-upload,C#,Asp.net,.net,File Upload,Multiple File Upload,我正在尝试使用FileUpload控件上载多个图像,但无法执行此操作 我试过: <asp:FileUpload ID="fuImage" AllowMultiple="true" runat="server"/> 但是它不起作用。。不知道如何用最简单的方法做这件事。试试这个 protected void btnAdd_Click(object sender, EventArgs e) { if (Request.Files.C
FileUpload
控件上载多个图像,但无法执行此操作
我试过:
<asp:FileUpload ID="fuImage" AllowMultiple="true" runat="server"/>
但是它不起作用。。不知道如何用最简单的方法做这件事。试试这个
protected void btnAdd_Click(object sender, EventArgs e)
{
if (Request.Files.Count > 0)
{
HttpFileCollection attachments = Request.Files;
for (int i = 0; i < attachments.Count; i++)
{
HttpPostedFile attachment = attachments[i];
if (attachment.ContentLength > 0 && !String.IsNullOrEmpty(attachment.FileName))
{
string fname = attachment.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")
{
attachment.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 = "";
}
}
}
}
}
}
protectedvoidbtnadd\u单击(对象发送者,事件参数e)
{
如果(Request.Files.Count>0)
{
HttpFileCollection attachments=Request.Files;
对于(int i=0;i0&&!String.IsNullOrEmpty(attachment.FileName))
{
字符串fname=attachment.FileName;
字符串路径=Server.MapPath(“~/EventPics/”);
字符串fext=Path.GetExtension(fname);
fext=fext.ToLower();
字符串链接=“~/EventPics/”+fname;
如果(fext=“.jpg”| | fext==”.png“| | fext==”.gif“| | fext==”.bmp”)
{
附件.SaveAs(路径+fname);
con=新的SqlConnection(ConfigurationManager.ConnectionString[“WebAAERT\u DBConnectionString”].ConnectionString);
SqlCommand命令cmd;
//创建命令
cmd=新的SqlCommand(“EventMasterInsert”,con);
cmd.CommandType=CommandType.storedProcess;
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);
//开放连接
cmd.Connection=con;
con.Open();
//执行命令
int rowcount=cmd.ExecuteNonQuery();
如果(行计数>0)
{
响应。写入(“警报(‘添加的事件’);”;
txtDesc.Text=“”;
txtEventDate.Text=“”;
txtEventTitle.Text=“”;
txtPlace.Text=“”;
txtShort.Text=“”;
txtTime.Text=“”;
}
}
}
}
}
}
试试这个
protected void btnAdd_Click(object sender, EventArgs e)
{
if (Request.Files.Count > 0)
{
HttpFileCollection attachments = Request.Files;
for (int i = 0; i < attachments.Count; i++)
{
HttpPostedFile attachment = attachments[i];
if (attachment.ContentLength > 0 && !String.IsNullOrEmpty(attachment.FileName))
{
string fname = attachment.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")
{
attachment.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 = "";
}
}
}
}
}
}
protectedvoidbtnadd\u单击(对象发送者,事件参数e)
{
如果(Request.Files.Count>0)
{
HttpFileCollection attachments=Request.Files;
对于(int i=0;i0&&!String.IsNullOrEmpty(attachment.FileName))
{
字符串fname=attachment.FileName;
字符串路径=Server.MapPath(“~/EventPics/”);
字符串fext=Path.GetExtension(fname);
fext=fext.ToLower();
字符串链接=“~/EventPics/”+fname;
如果(fext=“.jpg”| | fext==”.png“| | fext==”.gif“| | fext==”.bmp”)
{
附件.SaveAs(路径+fname);
con=新的SqlConnection(ConfigurationManager.ConnectionString[“WebAAERT\u DBConnectionString”].ConnectionString);
SqlCommand命令cmd;
//创建命令
cmd=新的SqlCommand(“EventMasterInsert”,con);
cmd.CommandType=CommandType.storedProcess;
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);
//开放连接
cmd.Connection=con;
con.Open();
//执行命令
int rowcount=cmd.ExecuteNonQuery();
如果(行计数>0)
{
响应。写入(“警报(‘添加的事件’);”;
txtDesc.Text=“”;
txtEventDate.Text=“”;
txtEventTitle.Text=“”;
txtPlace.Text=“”;
txtShort.Text=“”;
txtTime.Text=“”;
}
}
}
}
}
}
您不能使用.net framework 4.0。多文件上传是HTML的一项功能。文件输入控件的倍数
foreach(item in fuImage.postedFile)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (Request.Files.Count > 0)
{
HttpFileCollection attachments = Request.Files;
for (int i = 0; i < attachments.Count; i++)
{
HttpPostedFile attachment = attachments[i];
if (attachment.ContentLength > 0 && !String.IsNullOrEmpty(attachment.FileName))
{
string fname = attachment.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")
{
attachment.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 = "";
}
}
}
}
}
}