Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ross Ekberg 130 posts 381 karma points
    May 12, 2022 @ 18:11
    Ross Ekberg
    0

    Image Upload API

    I am trying to create an image upload API. I am new to MVC, so I'm not sure exactly how it all works.

    I setup the following server-side code, saved in the App_Code folder:

    namespace My.Controllers
    {
        [Umbraco.Web.Mvc.PluginController("My")]
        public class NewsletterImageUploadAPIController : UmbracoAuthorizedJsonController
        {
            public string UploadImage(HttpPostedFileBase file)
            {
                file.SaveAs(HttpContext.Current.Request.PhysicalApplicationPath + "media/websubscriptions/newsletterimages/" + file.FileName);
                return a json object
            }
        }
    }
    

    My intent is to return a json value, but I don't exactly know how to do that. But that's not my biggest problem. I am calling this API from TinyMCE 5 with the following initialization:

      tinymce.init({
          selector: 'textarea',
          relative_urls: false,
          convert_urls: false,
          plugins: "table,image,paste",
          encoding: "xml",
          paste_as_text: true,
          paste_data_images: true,
          images_upload_url: "/umbraco/backoffice/My/NewsletterImageUploadAPI/UploadImage"
      });
    

    The routing seems to work just fine. Unfortunately, when I try to upload an image, I get a 415 error, Unsupported Media Type. And I don't have any idea how to resolve it.

    Does anyone have any ideas?

  • Ross Ekberg 130 posts 381 karma points
    May 12, 2022 @ 18:41
    Ross Ekberg
    0

    I was able to find a bit more error information on this. It said that the HTTPPostedFileBase couldn't handle the multipart form data. So I removed it and requested the files directly with Request:

        public string UploadImage()
        {
            var file = HttpContext.Current.Request.Files;
            //file.SaveAs(HttpContext.Current.Request.PhysicalApplicationPath + "media/websubscriptions/newsletterimages/" + file.FileName);
            //return a json object
    
            return null;
        }
    

    Now, I am getting a 417 error: Missing token. Does anyone know how to solve that?

  • Ross Ekberg 130 posts 381 karma points
    May 17, 2022 @ 19:10
    Ross Ekberg
    100

    I changed "UmbracoAuthorizedJsonController" to "UmbracoAuthorizedApiControl" and it worked.

Please Sign in or register to post replies

Write your reply to:

Draft