我试图使用jQuery发布文件,但它返回'Not Found'和'undefined'。
HTML
<div class="form-group">
<label for="JQDocumentUploadTB">Optional - Upload Invoice (PDF)</label>
<div class="form-control-static">
<span class="btn btn-success fileinput-button">
<span>Add File...</span>
<input type="file" id="JQDocumentUploadTB" name="JQUpload" />
</span>
</div>
</div>
脚本
<script>
$('#JQDocumentUploadTB').on('change', function (e) {
var files = e.target.files;
if (files.length > 0) {
if (window.FormData !== undefined) {
var vdata = new FormData();
for (var x = 0; x < files.length; x++) {
vdata.append('file' + x, files[x]);
}
$.ajax({
type: "POST",
url: "UploadInvoice/UploadingFile",
contentType: false,
processData: false,
data: vdata,
success: function (result) {
alert(result);
},
error: function (xhr, status, p3, p4) {
var err = status + ' - ' + p3 + ' - ' + p4;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).Message;
alert(err);
}
});
} else {
alert("This browser doesn't support HTML5 file uploads!");
}
}
});
控制器
<HttpPost()>
Public Async Function UploadingFile() As Threading.Tasks.Task(Of JsonResult)
Try
For Each file As String In Request.Files
Dim fileContent = Request.Files(file)
If fileContent IsNot Nothing AndAlso fileContent.ContentLength > 0 Then
' Return ModalSuccess("File is here")
Return Json("Got here")
End If
Next
'Return ModalError("No file was uploaded!")
Catch ex As Exception
' Return ModalError("There was an error uploading the file")
Return Json("Upload failed")
End Try
End Function
答案 0 :(得分:0)
因为Ajax.BeginForm可以正常使用该URL ...
@Using (Ajax.BeginForm(Nothing, "UploadInvoice/PartialPost", Nothing, New AjaxOptions With {.HttpMethod = "POST"}, New With {.id = "FormUpload"}))
我曾假设同样适用于jQuery Ajax调用。傻我!将URL更改为
url: "../UploadInvoice/UploadingFile",
解决了这个问题,它现在可以正确回发 - 但是我预计这会在部署应用时出现问题,但是当我到达那里时我可以解决这个问题
答案 1 :(得分:0)
我认为更好的方法是不对您的网址进行硬编码,而是使用:
'@Url.Action("UploadingFile","UploadInvoice")'