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?
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:
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:
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
Can anyone look into this? I would really appreciate any help.
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):
However still don't understand why wouldn't standard AgularJS $http.post work.
is working on a reply...
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.