我试图在webforms中上传带有ajax和vb.net的文件。我目前正在从ajax获得成功响应,但我实际上从未在后端遇到断点,也没有上传或停止代码执行。这是我第一次尝试这个,所以我不确定如何专门处理后端,我在网上找到的所有东西都不符合我的确切规格。该文件需要作为varbinary直接上传到db列。几乎所有文件都将少于256kb,因此我决定不将它们存储在文件系统中。
<DataTemplate>
<Grid>
<TextBlock Visibility="Collapsed" Name="Hyperlink">
<Hyperlink NavigateUri="{Binding Data.path}">
</Hyperlink>
</TextBlock>
<TextBlock Visibility="Collapsed" Name="simpleText"
Text="{Binding Data.path}"></TextBlock>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Data.IsValidURL}" //your boolean property
Value="True">
<Setter TargetName="Hyperlink"
Property="Vibility"
Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding Data.IsValidURL}" //your boolean property
Value="False">
<Setter TargetName="simpleText"
Property="Vibility"
Value="Visible" />
</DataTrigger>
<DataTemplate.Triggers>
</DataTemplate>
<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
});
});
答案 0 :(得分: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
});