Ajaxfileupload只上传一张图片

时间:2014-03-07 07:12:49

标签: asp.net vb.net ajaxcontroltoolkit

大家好我已经创建了一个ajax文件上传,但是只将第一张图片导入我的数据库,请帮助解决这个问题,而且response.redirect也不能正常工作

Dim sellerid1 As Integer
    Using con1 As New SqlConnection(_start)
        Dim conss1 As New SqlConnection(_start)
        Dim sql2 As String = "SELECT SellerID FROM Seller INNER JOIN Member ON Seller.MemberID = Member.MemberID WHERE(Member.Username = @username)"
        Dim mycommand As New SqlCommand(sql2, conss1)
        conss1.Open()
        mycommand.Parameters.AddWithValue("@username", Session("user"))
        sellerid1 = Convert.ToInt32(mycommand.ExecuteScalar)
    End Using

    Dim filename As String = e.FileName
    Dim strDestPath As String = Server.MapPath("~/Seller/images/")
    AjaxFileUpload1.SaveAs(strDestPath & filename)

    Dim Imagepath As String = "~/Seller/images/" & filename
    Dim conss As New SqlConnection(_start)
    conss.Open()
    Dim sql1 As String = "SELECT TOP (1) ItemID FROM Item WHERE  (SellerID = @sellerid) ORDER BY ItemID DESC"
    Dim mycommand2 As New SqlCommand(sql1, conss)
    mycommand2.Parameters.AddWithValue("@sellerid", sellerid1)
    ItemID = mycommand2.ExecuteScalar

    Dim sql As String = "INSERT INTO Image(Image,ItemID) VALUES (@image,@item)"
    Dim myCommand1 = New SqlCommand(sql, conss)

    myCommand1.Parameters.AddWithValue("@image", Imagepath)
    myCommand1.Parameters.AddWithValue("@item", ItemID)
    myCommand1.ExecuteNonQuery()
    Response.Redirect("Views/Default2.aspx")

1 个答案:

答案 0 :(得分:0)

尝试使用UploadCompleteUploadCompleteEvent

如下所示:

 Protected Sub FileUpload_UploadComplete(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs) Handles FileUpload.UploadComplete

    Using con1 As New SqlConnection(_start)
    Dim conss1 As New SqlConnection(_start)
    Dim sql2 As String = "SELECT SellerID FROM Seller INNER JOIN Member ON Seller.MemberID = Member.MemberID WHERE(Member.Username = @username)"
    Dim mycommand As New SqlCommand(sql2, conss1)
    conss1.Open()
    mycommand.Parameters.AddWithValue("@username", Session("user"))
    sellerid1 = Convert.ToInt32(mycommand.ExecuteScalar)
    End Using

    Dim filename As String = e.FileName
    Dim strDestPath As String = Server.MapPath("~/Seller/images/")
    AjaxFileUpload1.SaveAs(strDestPath & filename)

    Dim Imagepath As String = "~/Seller/images/" & filename
    Dim conss As New SqlConnection(_start)
    conss.Open()
    Dim sql1 As String = "SELECT TOP (1) ItemID FROM Item WHERE  (SellerID = @sellerid) ORDER BY ItemID DESC"
    Dim mycommand2 As New SqlCommand(sql1, conss)
    mycommand2.Parameters.AddWithValue("@sellerid", sellerid1)
    ItemID = mycommand2.ExecuteScalar

    Dim sql As String = "INSERT INTO Image(Image,ItemID) VALUES (@image,@item)"
    Dim myCommand1 = New SqlCommand(sql, conss)

    myCommand1.Parameters.AddWithValue("@image", Imagepath)
    myCommand1.Parameters.AddWithValue("@item", ItemID)
    myCommand1.ExecuteNonQuery()
 End Sub

 Protected Sub FileUpload_UploadCompleteAll(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs) Handles FileUpload.UploadCompleteAll
    Response.Redirect("Views/Default2.aspx",False)
 End Sub