2021年5月4日星期二

ASP.NET Core 3.1 upload image with tinymce

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;            }  
https://stackoverflow.com/questions/67391693/asp-net-core-3-1-upload-image-with-tinymce May 05, 2021 at 04:22AM

没有评论:

发表评论