我真的很難讓 SummerNote 在 .NET 核心中上傳 iamge。問題是上傳新影像時 IFormFile 檔案引數為空。
我使用以下方法初始化 Summernote -
$('#MessageBody').summernote({
height: ($(window).height() - 300),
callbacks: {
onImageUpload: function(image) {
uploadImage(image[0]);
}
}
});
這是uploadImage函式 -
function uploadImage(image) {
var data = new FormData();
data.append("image", image);
$.ajax({
url: '/EmailTemplate/UploadImage',
cache: false,
contentType: false,
processData: false,
data: data,
type: "post",
success: function(url) {
var image = $('<img>').attr('src', 'http://' url);
$('#MessageBody').summernote("insertNode", image[0]);
alert(url);
var imgNode = document.createElement('img');
imgNode.src = url;
$('#MessageBody').summernote('insertNode', imgNode);
},
error: function(data) {
console.log(data);
}
});
最后,這是控制器 -
[HttpPost]
public async Task<IActionResult> UploadImage(IFormFile file)
{
string message;
var saveimg = Path.Combine(_hostingEnvironment.WebRootPath, "Images", file.FileName);
string imgext = Path.GetExtension(file.FileName);
if (imgext == ".jpg" || imgext == ".png")
{
using (var uploadimg = new FileStream(saveimg, FileMode.Create))
{
await file.CopyToAsync(uploadimg);
message = "The selected file" file.FileName " is saved";
}
}
else
{
message = "only JPG and PNG extensions are supported";
}
// return "filename : " saveimg " message :" message;
return Content(Url.Content(saveimg));
}
uj5u.com熱心網友回復:
file當欄位名稱為 時呼叫該引數image。要解決此問題,請使用相同的名稱,檔案或影像。
IFormFile型別表示欄位的值input type=file。IFormFile引數根據其名稱系結到欄位。同一表單中可能有許多檔案欄位,因此型別不足以確定欄位。
欄位系結在Model Binding檔案的Sources部分進行了說明。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/479174.html
上一篇:如何將引數傳遞給MVC控制器?
