2016-08-19 89 views
0

我試圖上傳一個文件與web表單中的ajax和vb.net。我目前正在從ajax獲得成功響應,但我從來沒有在後端實際觸及斷點,也沒有任何內容正在上傳或停止執行代碼。這是我第一次嘗試這個,所以我不知道如何專門處理後端,我在網上找到的所有東西都不符合我的確切規格。該文件需要作爲varbinary直接上傳到db列中。幾乎所有的文件都將小於256kb,因此我決定不將它們存儲在文件系統中。FormData的Ajax發佈是成功,但沒有擊中VB.Net功能

<input class="pull-left" type="file" id="fileToUpload"/> 
<button type="button" class="btn btn-primary" data-action="uploadDoc">Upload</button> 


uploadButton.on("click", function() { 
    var form = new FormData(); 
    var inputFile = document.getElementById("fileToUpload"); 
    form.append("file", inputFile.files[0]); 
    alert(inputFile.files.length); 
    $.ajax({ 
     url: '../secure/shipments.aspx/UploadFile', 
     data: form, 
     processData: false, 
     contentType: false, 
     type: 'POST', 
     success: function (data) { 
      alert(data); 
     }, 
     error: function (xhr, textStatus, errorThrown) { 
      alert("There was an error uploading the file. " + xhr.status + ': ' + errorThrown); 
     }, 
     cache: false 
    }); 
}); 


<System.Web.Services.WebMethod()> 
    Public Shared Sub UploadFile() 
     Try 
      Dim file As HttpPostedFile = HttpContext.Current.Request.Files("file") 
      Dim fname As String = Path.GetFileName(file.FileName) 
      Dim ftype As String = file.ContentType 
      Dim sDatasource As String = String.Empty 
      Dim inputArray(flen) As Byte 
      Dim myStream As System.IO.Stream 
      If (Current.Request.Files.Count > 1) Then 
       file = HttpContext.Current.Request.Files("file") 
       fname = Path.GetFileName(file.FileName) 
       ftype = file.ContentType 
       flen = file.ContentLength 

       myStream = file.InputStream 
       'read the file into the byte array 
       myStream.Read(inputArray, 0, flen) 
      End If 
      If Not HttpContext.Current.Session.Contents("datasource") Is Nothing Then sDatasource = HttpContext.Current.Session.Contents("datasource").ToString() 
      Using con As New SqlConnection(sDatasource) 
       Using cmd As New SqlCommand 
        cmd.CommandText = "test_insertDoc" 
        cmd.CommandType = CommandType.StoredProcedure 
        cmd.Parameters.AddWithValue("referenceno", 0) 
        cmd.Parameters.AddWithValue("doctype", ftype) 
        cmd.Parameters.AddWithValue("line", 0) 
        cmd.Parameters.Add("uploadedfile", SqlDbType.VarBinary, -1).Value = inputArray 
        cmd.Parameters.AddWithValue("customer", 0) 
        cmd.Parameters.AddWithValue("warehouse", 0) 
        cmd.Connection = con 
        con.Open() 
        cmd.ExecuteNonQuery() 
        con.Close() 
        cmd.Dispose() 
       End Using 
      End Using 
     Catch ex As Exception 
     End Try 
    End Sub 

DB結構:
DB Structure

回答

0

好吧,原來你不能或不知道如何發佈文件到.aspx頁面,所以我恩創建一個.ashx處理程序頁面。

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 
     Dim postedfile As HttpPostedFile = context.Request.Files("file") 
     Dim ftype As String = postedfile.ContentType 
     Dim flen As Integer = postedfile.ContentLength 
     Dim mystream As System.IO.Stream = postedfile.InputStream 
     Dim inputArray(flen) As Byte 
     Dim datasource As String = String.Empty 

     mystream.Read(inputArray, 0, flen) 

     If Not HttpContext.Current.Session.Contents("datasource") Is Nothing Then datasource = HttpContext.Current.Session.Contents("datasource").ToString() 
     Try 
      Using con As New SqlConnection(datasource) 
       Using cmd As New SqlCommand 
        cmd.CommandText = "test_insertDoc" 
        cmd.CommandType = CommandType.StoredProcedure 
        cmd.Parameters.AddWithValue("referenceno", "2") 
        cmd.Parameters.AddWithValue("doctype", ftype) 
        cmd.Parameters.AddWithValue("line", 0) 

        cmd.Parameters.Add("uploadedFile", SqlDbType.VarBinary, -1).Value = inputArray 

        cmd.Parameters.AddWithValue("customer", "0") 
        cmd.Parameters.AddWithValue("warehouse", "0") 
        cmd.Connection = con 
        con.Open() 
        cmd.ExecuteNonQuery() 
        con.Close() 
        cmd.Dispose() 
       End Using 
      End Using 
     Catch ex As Exception 
      Dim message As String = ex.Message 
     End Try 
End Sub 


$.ajax({ 
    data: form, 
    processData: false, 
    contentType: false, 
    type: 'POST', 
    datatype: 'json', 
    url: '../secure/FileUpload.ashx', 
    success: function (data) { 

    }, 
    error: function (xhr, textStatus, errorThrown) { 
     alert("There was an error uploading the file. " + xhr.status + ': ' + errorThrown); 
    }, 
    cache: false 
});