I'm trying to set up the image upload with tinymce but I can't get the image in the controller from the view that contains the form.
I obtaine this error message :
System.NullReferenceException : 'Object reference not set to an instance of an object.'
The images should be stored in the images folder which is in the wwwroot
js file :
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();
}
Controller :
[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;
}
Change imgfile
to file
public async Task<string> uploadImg(IFormFile file)
{
//....
}