c#结合miniexcle实现文件的上传
[HttpPut("import")]
[AllowAnonymous]
public IActionResult UploadFile()
{
var file = Request.Form.Files["file"];
if (file == null)
{
return BadRequest("文件未找到");
}
if (!file.FileName.Equals("订单信息导入.xlsx"))
{
return BadRequest("请上传正常的文件");
}
try
{
var stream = new MemoryStream();
file.CopyTo(stream);
var imList = stream.Query<ImportOrder>().ToList();
var adminName = HttpContext.GetName();
var res = _BOrderService.InsertImport(imList, adminName);
return Ok(res);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
前端vue 代码如下
//上传文件
handleChanges(file) {
this.file = file.raw;
},
async uploadFile() {
const formData = new FormData();
formData.append("file", this.file, this.file.name);
console.log(formData, ' formData')
try {
const response = await fetch("/dev-api/plan/BOrder/import", {
method: "put",
body: formData,
});
if (response.ok) {
this.msgSuccess("文件上传成功");
this.getList()
this.getOrderNoList()
this.file = null ;
} else {
this.msgWarning("上传失败,请检查文档: " + response.statusText);
this.file = null ;
}
} catch (err) {
this.msgwarining("上传失败,请检查文档: " + err);
this.file = null ;
}
},
这样就可以啦! 记得导入miniexcle 的插件