ASP.NET Core 3.1用tinymce上传图片
ASP.NET Core 3.1 upload image with tinymce
我正在尝试使用 tinymce 设置图像上传,但我无法从包含表单的视图中获取控制器中的图像。
我收到此错误消息:
System.NullReferenceException : 'Object reference not set to an
instance of an object.'
图像应存储在 wwwroot 中的图像文件夹中
js 文件:
tinymce.init({
selector: '#TinyArea',
skin: 'oxide',
content_css: 'default',
toolbar: 'undo redo styleselect bold italic alignleft aligncenter alignright bullist numlist outdent indent table lists code link | fullscreen preview save print | insertfile image media anchor',
plugins: 'code emoticons imagetools preview print autosave save image link media table anchor lists checklist wordcount imagetools paste ',
image_caption: true,
autosave_interval: '30s',
image_title: true,
paste_data_images: true,
automatic_uploads: true,
images_upload_url: '/PropositionArticle/uploadImg',
file_picker_types: 'image',
file_picker_callback: function (cb, value, meta) {
var input = document.createElement('input');
input.setAttribute('type', 'file');
input.setAttribute('accept', 'image/*');
input.onchange = function () {
var file = this.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
var id = 'blobid' + (new Date()).getTime();
var blobCache = tinymce.activeEditor.editorUpload.blobCache;
var base64 = reader.result.split(',')[1];
var blobInfo = blobCache.create(id, file, base64);
blobCache.add(blobInfo);
cb(blobInfo.blobUri(), { title: file.name });
};
};
input.click();
}
控制器:
[HttpPost]
public async Task<string> uploadImg(IFormFile imgfile)
{
string message;
var saveimg = Path.Combine(webhost.WebRootPath, "Images", imgfile.FileName);
string imgext = Path.GetExtension(imgfile.FileName);
if (imgext == ".jpg" || imgext == ".png")
{
using (var uploadimg = new FileStream(saveimg, FileMode.Create))
{
await imgfile.CopyToAsync(uploadimg);
message = "The selected file" + imgfile.FileName + " is save";
}
}
else
{
message = "only JPG and PNG extensions are supported";
}
return "filename : " + saveimg + " message :" + message;
}
将imgfile
更改为file
public async Task<string> uploadImg(IFormFile file)
{
//....
}
我正在尝试使用 tinymce 设置图像上传,但我无法从包含表单的视图中获取控制器中的图像。
我收到此错误消息:
System.NullReferenceException : 'Object reference not set to an instance of an object.'
图像应存储在 wwwroot 中的图像文件夹中
js 文件:
tinymce.init({
selector: '#TinyArea',
skin: 'oxide',
content_css: 'default',
toolbar: 'undo redo styleselect bold italic alignleft aligncenter alignright bullist numlist outdent indent table lists code link | fullscreen preview save print | insertfile image media anchor',
plugins: 'code emoticons imagetools preview print autosave save image link media table anchor lists checklist wordcount imagetools paste ',
image_caption: true,
autosave_interval: '30s',
image_title: true,
paste_data_images: true,
automatic_uploads: true,
images_upload_url: '/PropositionArticle/uploadImg',
file_picker_types: 'image',
file_picker_callback: function (cb, value, meta) {
var input = document.createElement('input');
input.setAttribute('type', 'file');
input.setAttribute('accept', 'image/*');
input.onchange = function () {
var file = this.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
var id = 'blobid' + (new Date()).getTime();
var blobCache = tinymce.activeEditor.editorUpload.blobCache;
var base64 = reader.result.split(',')[1];
var blobInfo = blobCache.create(id, file, base64);
blobCache.add(blobInfo);
cb(blobInfo.blobUri(), { title: file.name });
};
};
input.click();
}
控制器:
[HttpPost]
public async Task<string> uploadImg(IFormFile imgfile)
{
string message;
var saveimg = Path.Combine(webhost.WebRootPath, "Images", imgfile.FileName);
string imgext = Path.GetExtension(imgfile.FileName);
if (imgext == ".jpg" || imgext == ".png")
{
using (var uploadimg = new FileStream(saveimg, FileMode.Create))
{
await imgfile.CopyToAsync(uploadimg);
message = "The selected file" + imgfile.FileName + " is save";
}
}
else
{
message = "only JPG and PNG extensions are supported";
}
return "filename : " + saveimg + " message :" + message;
}
将imgfile
更改为file
public async Task<string> uploadImg(IFormFile file)
{
//....
}