简体   繁体   中英

how to select multiple images using FileUpload conrol in framework 4.0 in asp.net c#

I am trying to upload more than one image using FileUpload control but not able to do it

i tried:

<asp:FileUpload ID="fuImage" AllowMultiple="true" runat="server"/>

and this also :

<asp:FileUpload ID="fuImage" multiple="multiple" runat="server"/>

but as i searched for hours on google,i found that this is only working for version 4.5 not in 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 = "";


                }
            }
        }

i tried

 foreach(item in fuImage.postedFile)
{
}

but its not working.. no idea how to do it in most easy way..

try this

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


                            }
                        }
                    }
                }
            }
        }

You can't with .net framework 4.0 . Multiple file upload is a feature of HTML. Multiples for the file input control were only just added in HTML 5, ASP.Net 4.0 didn't have this feature. You'll have to find a third-party control which does not inherit from the fileuplaod control in order to do this.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM