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