如何使用ASP.NET中的ajaxfileupload将多个文件上传到数据库? [英] How to upload multiple files using ajaxfileupload in ASP.NET to database?

查看:71
本文介绍了如何使用ASP.NET中的ajaxfileupload将多个文件上传到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我一直在尝试使用asp.net中的ajaxfileupload将六个图像上传到MSSQL服务器,但它总是只保存一个图像。请帮助我解决这个问题。你也可以为实现同一目标提供更好的选择。



我尝试过:



这是aspx代码:



< div  class  =   divTableBody >  
< div class = divTableRow >
< div class = divTableCell > 缩略图图片:
< / div >
< div cl ass = divTableCell >
< asp:AjaxFileUpload ID = itemFileUpload3
runat = server
OnUploadComplete = itemImage3OnUpload
MaximumNumberOfFiles = 10宽度= 420px />
< p id = itemImage3Validate > < / p >
< / div >
< / div >
< / div >





这里是aspx.cs代码:



  protected   void  itemImage3OnUpload( object  sender,AjaxFileUploadEventArgs 
e)
{

string filename = e.FileName;
会话[ PicturePath3] = filename;
if (itemType1.Checked)
{
itemFileUpload3.SaveAs(Server.MapPath( 〜/ Images / Sub Images /)+
filename);
}
其他 if (itemType2.Checked)
{
itemFileUpload3.SaveAs(Server.MapPath( 〜/ Images / Sub Images /)+
filename);
}

}

受保护 void itemSaveButton_Click( object sender,EventArgs e)
{

try
{

string itemThumbImage = Session [ < span class =code-string> PicturePath3]。ToString();


int itemInstrumentID =
ConnectionClassGuitarItems.GetItemIDByNameAndModel
(item_brandId,item_model);

var subImg = new thumbnailImage
{
instrumentId = itemInstrumentID,
subimages = itemThumbImage
};


ConnectionClassGuitarItems.AddThumnailImage(subImg);
ScriptManager.RegisterStartupScript( this .Page, this .GetType()
msgboxScc btnClickSuccess(); true );

ClearTextFields2();

}
catch (例外)
{
ScriptManager.RegisterStartupScript( this .Page, this .GetType()
msgboxErr btnClickFail(); true );
}


}





然后我将使用此添加我的图像代码:



  public   static   void  AddThumnailImage(thumbnailImage subImg)
{
using (MusicStoreDBEntities obj = new MusicStoreDBEntities())
{

obj.thumbnailImages.Add(subImg);
obj.SaveChanges();
}
}

解决方案

UploadedComplete 是你服务器上的事件应该听听:

Quote:

成功上传文件时在服务器上引发。在这种情况下,AjaxFileUploadEventArgs的实例在包含文件名,大小和内容类型的参数中传递。



有关控件事件的更多信息:AjaxFileUpload示例上传多个文件 - CodingFusion [ ^ ]



注意:谷歌搜索是你的朋友 - 那里有很多信息...我是一名MVC程序员,所以不得不仔细查看。上述信息是使用以下信息找到的: asp.net ajaxfileupload示例 - Google搜索 [ ^ ]


请尝试下面的代码



protected void ajaxUpload1_UploadComplete(object sender,AjaxControlToolkit.AjaxFileUploadEventArgs e)

{

id + = 1;

string filename = Path.GetFileName(e.FileName);

string filepath = Server。 MapPath(〜/ Images / Gallery /+ filename);

ajaxUpload1.SaveAs(filepath);



string Insert =Insert进入滑块(滑动,slurl)值(+ @id + ,'+ @ IMAGE_PATH +');

SqlCommand cmd = new SqlCommand(Insert,con);

cmd.CommandType = CommandType.Text;

try

{

con.Open();

cmd.ExecuteNonQuery();

}

catch(例外情况ex)

{

Response.Write(ex.Message);

}

终于

{

con.Close();

cmd.Dispose();

}

}


Hi guys! I'v been trying to upload six images using ajaxfileupload in asp.net to MSSQL server but it always save only one image. Kindly help me on solving this one. You can also provide better alternatives on achieving the same goal.

What I have tried:

Here is the aspx code:

<div class="divTableBody">
                  <div class="divTableRow">
                     <div class="divTableCell">Thumbnail Images:
                       </div>
                        <div class="divTableCell"> 
                           <asp:AjaxFileUpload ID="itemFileUpload3" 
                           runat="server" 
                           OnUploadComplete="itemImage3OnUpload" 
                           MaximumNumberOfFiles="10" Width="420px"/>
                           <p  id="itemImage3Validate"></p> 
                       </div>
                  </div>
              </div>   



And here is the aspx.cs code:

protected void itemImage3OnUpload(object sender, AjaxFileUploadEventArgs 
      e)
    {

    string filename = e.FileName;
    Session["PicturePath3"] = filename;
    if (itemType1.Checked)
    {
        itemFileUpload3.SaveAs(Server.MapPath("~/Images/Sub Images/") +  
   filename);
    }
    else if (itemType2.Checked)
    {
        itemFileUpload3.SaveAs(Server.MapPath("~/Images/Sub Images/") + 
   filename);
    }

}

   protected void itemSaveButton_Click(object sender, EventArgs e)
   {

    try
    {

        string itemThumbImage = Session["PicturePath3"].ToString();


        int itemInstrumentID = 
       ConnectionClassGuitarItems.GetItemIDByNameAndModel
             (item_brandId,item_model);

        var subImg = new thumbnailImage
        {
            instrumentId = itemInstrumentID,
            subimages = itemThumbImage
        };


        ConnectionClassGuitarItems.AddThumnailImage(subImg);
        ScriptManager.RegisterStartupScript(this.Page,this.GetType()
       , "msgboxScc", "btnClickSuccess();", true);

        ClearTextFields2();

    }
    catch (Exception)
    {
        ScriptManager.RegisterStartupScript(this.Page, this.GetType()
     , "msgboxErr", "btnClickFail();", true);
    }


   } 



And then I will add my images using this code:

public static void AddThumnailImage(thumbnailImage subImg)
{
    using (MusicStoreDBEntities obj = new MusicStoreDBEntities())
    {

        obj.thumbnailImages.Add(subImg);
        obj.SaveChanges();
    }
}

解决方案

UploadedComplete is the event on the server that you should be listen to:

Quote:

Raised on the server when a file is uploaded successfully. In this event an instance of AjaxFileUploadEventArgs is passed in the argument that contains file name, size and content type.


More information about the control's events here: AjaxFileUpload example to upload multiple files - CodingFusion[^]

NOTE: Google Search is your friend - lots of information out there... I'm a MVC programmer, so had to look this up. The information above was found using this: asp.net ajaxfileupload example - Google Search[^]


please try below code

protected void ajaxUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
id += 1;
string filename = Path.GetFileName(e.FileName);
string filepath = Server.MapPath("~/Images/Gallery/" + filename);
ajaxUpload1.SaveAs(filepath);

string Insert = "Insert into slider (slid,slurl) values (" + @id + ",'" + @IMAGE_PATH + "')";
SqlCommand cmd = new SqlCommand(Insert, con);
cmd.CommandType = CommandType.Text;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
cmd.Dispose();
}
}


这篇关于如何使用ASP.NET中的ajaxfileupload将多个文件上传到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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