使用asp.net上传多个图像? [英] Upload multiple images using asp.net?

查看:62
本文介绍了使用asp.net上传多个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

使用asp.net上传多个图片



这里上传图片工作正常,但选择一张图片不能正常工作请以任何其他方式或示例回复我。



Dear All,
Upload multiple images using asp.net

Here uploading with images working properly but select one image not working please reply me any another way or examples.

protected void btnPostAd_Click(object sender, EventArgs e)
    {
        if (ddlCategoryType.SelectedItem.Text != "--Select--")
        {
            if (FileUpload1.PostedFile.ContentLength != 0 && FileUpload2.PostedFile.ContentLength != 0)
            {
                string strimage = @"~\ProductImage\" + FileUpload1.FileName;
                string strimage2 = @"~\ProductImage\" + FileUpload2.FileName;
                FileUpload1.PostedFile.SaveAs(Server.MapPath(strimage));
                FileUpload2.PostedFile.SaveAs(Server.MapPath(strimage2));
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                String strInsert = "insert into ProductDetailsTable1(CategoriesType,Title,Description,State,City,ContactPhoto,ContactSPhoto,Price,ContactName,ContactEmail,ContactMobile) values('" + ddllist.Text + "','" + txtTitle.Text + "','" + txtDescription.Text + "','" + ddlState.SelectedItem.Text + "','" + ddlCity.SelectedItem.Text + "','" + strimage + "','" + strimage2 + "','" + txtPrice.Text + "','" + txtContactName.Text + "','" + txtContactEmail.Text + "','" + txtContactMobile.Text + "',)";
                SqlCommand cmd = new SqlCommand(strInsert, con);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Posted Successfully')", true);
                    
                }
                catch (SqlException ex)
                {
                    string errorMessage = "Error in entering competition";
                    errorMessage += ex.Message;
                    throw new Exception(errorMessage);
                }
                finally
                {
                    con.Close();
                }
                Clear();
            }
            else
            {
                    string strimage = "NoImage.jpg";
                    string strimage2 = "NoImage.jpg";
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                    String strInsert = "insert into ProductDetailsTable1(CategoriesType,Title,Description,State,City,ContactPhoto,ContactSPhoto,Price,ContactName,ContactEmail,ContactMobile) values('" + ddllist.Text + "','" + txtTitle.Text + "','" + txtDescription.Text + "','" + ddlState.SelectedItem.Text + "','" + ddlCity.SelectedItem.Text + "','" + strimage + "','" + strimage2 + "','" + txtPrice.Text + "','" + txtContactName.Text + "','" + txtContactEmail.Text + "','" + txtContactMobile.Text + "')";
                    SqlCommand cmd = new SqlCommand(strInsert, con);
                    try
                    {
                        con.Open();
                        cmd.ExecuteNonQuery();
                        
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Posted Successfully')", true);
                        
                    }
                    catch (SqlException ex)
                    {
                        string errorMessage = "Error in entering competition";
                        errorMessage += ex.Message;
                        throw new Exception(errorMessage);
                    }
                    finally
                    {
                        con.Close();
                    }
                    Clear();
                }
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Select CategoryType')", true);
        }
    }}

推荐答案

试试这个



try this

HttpFileCollection hfc = Request.Files;
 for (int i = 0; i < hfc.Count; i++)
       {
           HttpPostedFile hpf = hfc[i];
           if (hpf.ContentLength > 0)
           {
               string FileName = hpf.FileName;
                   hpf.SaveAs(SavePath + "\\" + Path.GetFileName(FileName));

           }
           }







HttpFileCollection提供对客户上传的文件的访问和组织。




HttpFileCollection Provides access to and organizes files uploaded by a client.


Just Copy&将此代码粘贴到您的页面上..默认它会运行



Just Copy & Paste This code on Your Page .. Defiantly it will Run

if (ddlCategoryType.SelectedItem.Text != "--Select--")
        {
            string strimage2 = "NoImage.jpg";
            string strimage = "NoImage.jpg";
            if (FileUpload1.PostedFile.ContentLength != 0)
            {
                strimage= @"~\ProductImage\" + FileUpload1.FileName;
                FileUpload1.PostedFile.SaveAs(Server.MapPath(strimage));
            }
            if (FileUpload2.PostedFile.ContentLength != 0 ) 
            {
                strimage2 = @"~\ProductImage\" + FileUpload2.FileName;
                FileUpload1.PostedFile.SaveAs(Server.MapPath(strimage2));

            }
            if(FileUpload1.HasFile || FileUpload2.HasFile)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                String strInsert = "insert into ProductDetailsTable1(CategoriesType,Title,Description,State,City,ContactPhoto,ContactSPhoto,Price,ContactName,ContactEmail,ContactMobile) values('" + ddllist.Text + "','" + txtTitle.Text + "','" + txtDescription.Text + "','" + ddlState.SelectedItem.Text + "','" + ddlCity.SelectedItem.Text + "','" + strimage + "','" + strimage2 + "','" + txtPrice.Text + "','" + txtContactName.Text + "','" + txtContactEmail.Text + "','" + txtContactMobile.Text + "',)";
                SqlCommand cmd = new SqlCommand(strInsert, con);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();

                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Posted Successfully')", true);

                }
                catch (SqlException ex)
                {
                    string errorMessage = "Error in entering competition";
                    errorMessage += ex.Message;
                    throw new Exception(errorMessage);
                }
                finally
                {
                    con.Close();
                }
                
            }
            else
            {
                 
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                String strInsert = "insert into ProductDetailsTable1(CategoriesType,Title,Description,State,City,ContactPhoto,ContactSPhoto,Price,ContactName,ContactEmail,ContactMobile) values('" + ddllist.Text + "','" + txtTitle.Text + "','" + txtDescription.Text + "','" + ddlState.SelectedItem.Text + "','" + ddlCity.SelectedItem.Text + "','" + strimage + "','" + strimage2 + "','" + txtPrice.Text + "','" + txtContactName.Text + "','" + txtContactEmail.Text + "','" + txtContactMobile.Text + "')";
                SqlCommand cmd = new SqlCommand(strInsert, con);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();

                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Posted Successfully')", true);

                }
                catch (SqlException ex)
                {
                    string errorMessage = "Error in entering competition";
                    errorMessage += ex.Message;
                    throw new Exception(errorMessage);
                }
                finally
                {
                    con.Close();
                }
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Select CategoryType')", true);
        }


这篇关于使用asp.net上传多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆