簡體   English   中英

如何在ASP.NET C#中的Framework 4.0中使用FileUpload conrol選擇多個圖像

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

我正在嘗試使用FileUpload控件上傳多個圖像,但無法做到這一點

我試過了:

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

這也:

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

但是當我在Google上搜索了幾個小時后,我發現這僅適用於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)
{
}

但它不起作用..不知道如何以最簡單的方式來做..

嘗試這個

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


                            }
                        }
                    }
                }
            }
        }

您不能使用.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM