【发布时间】:2018-09-20 02:14:18
【问题描述】:
检查下面的 jquery 代码。在这里,我从 html 中获取文件,然后通过 ajax 调用将其发布到我的 Controller Post 方法。如您所见,从 Controller post 方法中,我成功地在名为 files 的变量中接收了该文件。但我的问题是如何从这个 ajax 调用中发送另外两个名为 - type 和 id 的文本参数,那么我如何使用该文件从控制器获取该值?基本上我也必须用那些文本值来获取那个文件。这怎么可能? ajax 和控制器需要什么改变?
HTML:
<div class="col-sm-3" style="float:left;">
<label class="btn btn-outline-dark btn-block">
Browse...
<span id="uploaded-file-name" style="font-style: italic"></span>
<input id="file-upload" type="file" name="file"
onchange="$('#uploaded-file-name').text($('#file-upload')[0].value);" hidden>
</label>
</div>
<div class="col-sm-2" style="float:left;">
<button class="btn btn-dark" id="start_upload">Start Upload</button>
</div>
jQuery ajax:
//file upload
$("#start_upload").click(function (evt) {
var fileUpload = $("#file-upload").get(0);
var files = fileUpload.files;
var data = new FormData();
for (var i = 0; i < files.length; i++) {
data.append(files[i].name, files[i]);
}
$.ajax({
type: "POST",
url: "/Products/UploadFiles",
contentType: false,
processData: false,
data: data,
success: function (message) {
alert(message);
},
error: function () {
alert("There was error uploading files!");
}
});
});
MVC .net 核心控制器:
[HttpPost]
public IActionResult UploadFiles()
{
//file upload process
var files = Request.Form.Files;
string type = "";
int id = ;
}
【问题讨论】:
标签: c# jquery asp.net-core-mvc