【问题标题】:how to select multiple images using FileUpload conrol in framework 4.0 in asp.net c#如何在asp.net c#中使用Framework 4.0中的FileUpload控件选择多个图像
【发布时间】:2016-05-10 11:32:53
【问题描述】:

我正在尝试使用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)
{
}

但它不起作用..不知道如何以最简单的方式做到这一点..

【问题讨论】:

    标签: c# asp.net .net file-upload multiple-file-upload


    【解决方案1】:

    试试这个

            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 = "";
    
    
                                }
                            }
                        }
                    }
                }
            }
    

    【讨论】:

    • 我只能选择文件。所以这段代码也只会上传一张图片。
    • 我用过这个&lt;asp:FileUpload ID="fuImage" multiple="multiple" runat="server"/&gt;
    【解决方案2】:

    您不能使用 .net framework 4.0 。多文件上传是 HTML 的一项功能。文件输入控件的倍数只是在 HTML 5 中添加的,ASP.Net 4.0 没有此功能。您必须找到不从 fileuplaod 控件继承的第三方控件才能执行此操作。

    http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/AsyncFileUpload/AsyncFileUpload.aspx

    http://www.codeproject.com/Articles/24271/Multiple-File-Upload-User-Control

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多