0

I am using Umbraco and .net core app, to upload files via angularJs in the backoffice this working fine, but now I need to upload images to the media section of the umbraco.

vm.uploadFile = function () {
  let uploadUrl = `/umbraco/backoffice/api/MyApi/UploadFile`;

  let fileInput = document.getElementById('UploadFile');
  let file = fileInput.files[0];

  if (!file) {
    alert("Please select a file to upload.");
    return;
  }

  vm.isLoading = true;
  let formData = new FormData();
  formData.append('file', file);
  
  Upload.upload({
    url: uploadUrl,
    file: file
  }).success(function () {
    fileInput.value = '';
    alert('File uploaded successfully!');
  }).catch(function (x) {
    console.error('Error uploading file:', x);
    alert('File upload failed.');
  })
    .finally(() => {
    vm.isLoading = false;
  });
};

public class MyApiController
{

 [HttpPost]
    public IHttpActionResult UploadFile()
    {
       
            var httpRequest = HttpContext.Current.Request;

            if (httpRequest.Files.Count == 0)
                return BadRequest("No file uploaded.");

            var file = httpRequest.Files[0];
       ...
    }
}

How can I upload some images to the umbraco media, lets say I have a info folder there like "umbraco#/media/info" and want upload images using my custom code, and also need to know the URL of the umbraco media or the service responsible for that.

umbraco media

1
  • form what I got from your code you are uploading files to an Api Controller and you want to save these files under the media section in umbraco? for that you can inject the mediaService into your api controller and used it to save the files there. check the docs here Commented Apr 11 at 7:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.