Sargis
Sargis

Reputation: 147

Uploading files via angularJs in Umbraco Backoffice media folder

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

Upvotes: 0

Views: 20

Answers (0)

Related Questions