Copied to clipboard

Flag this post as spam?

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


  • Jesse Andrews 159 posts 636 karma points
    Oct 31, 2019 @ 02:06
    Jesse Andrews
    0

    How do I send file data to an UmbracoAuthorizedApiController in umbraco 8?

    I have the following logic that worked previously in umbraco 7.

    The API Request

    $http({
        method: 'POST',
        url: '/umbraco/backoffice/feed/feedapi/collectdata',
        headers: { 'Content-Type': undefined },
        transformRequest: function (data) {
            var formData = new FormData();
            formData.append("file", data.file);
            return formData;
        },
        data: {
            file: ctrl.settings.file
        }
    });
    

    The API

    public async Task<IHttpActionResult> CollectData() {
        if (!Request.Content.IsMimeMultipartContent()) {
            return BadRequest("Unsupported media type.");
        }
        var id = Guid.NewGuid();
        var uploadFolder = SetupFolders(id);
        var provider = new NamedMultipartFormDataStreamProvider(uploadFolder);
        var result = await Request.Content.ReadAsMultipartAsync(provider);
        var file = result.FileData.First();
        var path = $"{uploadFolder}\\{file.Headers.ContentDisposition.FileName.Replace("\"", "")}";
        var jsonData = ParseJson(System.IO.File.ReadAllText(path));
        return Ok(new JObject {
            {"id", id },
            {"link", jsonData["link"] },
            { "items", jsonData["items"] }
        });
    }
    

    All this api is doing is uploading a file selected with a file input to the server for further processing. The problem is that result.FileData has no data, so it fails. Any ideas?

    For reference, the submitted data looks like

    ------WebKitFormBoundaryJJZgcyycBGHiB2UX
    Content-Disposition: form-data; name="file"
    
    [object File]
    ------WebKitFormBoundaryJJZgcyycBGHiB2UX--
    

    when I submit to the api.

  • Jesse Andrews 159 posts 636 karma points
    Nov 02, 2019 @ 01:04
    Jesse Andrews
    100

    I was able to resolve this by using the Upload service that Umbraco has already installed. My final code ended up looking like.

    request = Upload.upload({
        url: '/umbraco/backoffice/feed/feedapi/collectdata',
        file: ctrl.settings.rawFile
    });
    

    The rawFile object is set with ng-model, as shown below.

    <input type="file" ng-model="importer.settings.rawFile" ngf-select ng-multiple="false" /
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies