Copied to clipboard

Flag this post as spam?

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


  • Ostap 3 posts 93 karma points
    Aug 28, 2020 @ 15:00
    Ostap
    0

    Post file with AngularJS stopped working after upgrading from 7.15.5 to 8.6.4

    I have custom dashboard which uses AngularJS. Here is the code I use to post file to ApiController:

    angular.module("umbraco").controller('myDriveDashboardController',
    function myDriveDashboardController($scope, $http) {
    
        $scope.uploadFile = function () {
            var fd = new FormData();
            fd.append('file', $scope.myFile);
    
            $http.post('/umbraco/api/AlarmsImportApi/ImportAlarms?culture=en-US', fd, {
                withCredentials: true,
                headers: {
                    'Content-Type': undefined
                },
                transformRequest: angular.identity
            }).then(function (response) {
                console.log(response);
            }, function (error) {
                console.log(error);
            });
        };
    });
    

    When this request reaches API controller method ImportAlarms the file is missing. It seems that FormData is not being correctly serialized to binary because in Chrome tools I can see that request content-type: is set to text/plain;charset=UTF-8. But as I understand it should be 'multipart/form-data'. Setting content-type manually to 'multipart/form-data' does not help. Here is my ApiController:

    public sealed class AlarmsImportApiController : UmbracoApiController
    {
        public AlarmsImportApiController() { }
    
        [HttpPost]
        public IHttpActionResult ImportAlarms(String culture)
        {
            HttpRequestBase httpRequest = UmbracoContext.HttpContext.Request;
            if (httpRequest.Files.Count == 0)
            {
                return StatusCode(HttpStatusCode.BadRequest);
            }
            return StatusCode(HttpStatusCode.OK);
        }
    }
    

    As you may guess I'm always hitting BadRequest as Files.Count == 0.

    This behavior is reproducible with brand new Umbraco 8.6.4 site. I have simply added custom dashboard and ApiController.

    Could you please share some thoughts on where I made a mistake?

    P.S. Created repository with my project for better understanding: https://github.com/omoravskyi/UmbracoPostFileIssue

  • Ostap 3 posts 93 karma points
    Sep 07, 2020 @ 07:16
    Ostap
    0

    Can anyone look into this? I would really appreciate any help.

  • Ostap 3 posts 93 karma points
    Sep 07, 2020 @ 09:53
    Ostap
    100

    I managed to resolve my issue by using Upload service (got the hint here https://our.umbraco.com/forum/umbraco-8/96894-content-app-with-file-upload-ability):

    const postFiles = function (files, endpoint) {
            return Upload.upload({
                url: endpoint,
                fields: {},
                file: files
            });
        }
    

    However still don't understand why wouldn't standard AgularJS $http.post work.

  • 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