前台js:
//原生 js
//文件流传输使用 FormData
var myform = new FormData();
myform.append("files", files[files[0]]);
var xhr = new XMLHttpRequest();
xhr.open("post", "文件上传路径", true);
//xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 这段不要加
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
//从服务器获得数据
console.log(xhr.response);
var result = JSON.parse(xhr.response);
if (result.Success) {
}
}
xhr.send(myform);
}
//ajax
var formData = new FormData();
formData.append('file',file);
formData.append('year', year);
formData.append('stations', JSON.stringify(stationArr));
$.ajax({
url: '',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (result) {
},
error: function (returndata) {
console.log(returndata);
}
});
}
后台接收:
public IActionResult GetUploadAttachment()
{
IFormFile[] files = _httpContextAccessor.HttpContext.Request.Form.Files.ToArray();
return Json("");
}