I am using an angular web project with c# web api with CORS enabled.
All of my CORS work correctly on all calls except when I make a file upload to a async Task. Here is the method I am referering to.
[HttpPost]
public async Task<HttpResponseMessage> UploadFile(){
    //code
}
The error I am getting is:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
So obviously I am not even allowed to call my UploadFile method.
On small files the call works and the 'Access-Control-Allow-Origin' is present on the response, but on larger files 60 mb + it is not.
I am using a XMLHttpRequest to make the web api call.
var xhr = new XMLHttpRequest();
        if (xhr.upload) {
            xhr.upload.filename = $scope.files[i].name;
            xhr.upload.addEventListener('progress', function (evt) {
                var percentComplete = Math.round(evt.loaded * 100 / evt.total);
                $scope.uploadCompleteValues[this.filename] = percentComplete;
                console.log(percentComplete + '%');
                if (!$scope.$$phase) { $scope.$apply(); } 
            }, false);
        }
        xhr.onreadystatechange = function (ev) {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    // Success! Response is in xhr.responseText
                    console.log('success upload of file');
                    }
                } else {
                    // Error! Look in xhr.statusText
                    console.log('failed to upload file');
                    console.log('error : ', xhr.statusText);
                    $scope.$root.$broadcast('NOTIFY-ERROR-1btn', {
                        text: 'Artwork failed to save.  ',
                        buttons: [
                            {
                                text: 'Ok'
                            }
                        ]
                    });
                }
            }
        }
        xhr.open('POST', webapiUrl + 'artwork/UploadFile', true);
        var cookie = $cookieStore.get('authToken');
        if (cookie) {
            xhr.setRequestHeader("auth-token", cookie.token);
        } else {
            $location.path('login');
        }
        var data = new FormData();
        data.append('OrderlineId', $scope.orderline.Id);
        data.append($scope.files[i].name + 1, $scope.files[i]);
        xhr.send(data);
Is it possible for the Cross Origin Resource Sharing request to timeout before the file is sent throught the xhr.send ?
Make sure your Max allowed content Length in your web.config is set high enough otherwise you'll get an error. Check your IIS logs to see if that's the issue.
      <requestFiltering>
    <requestLimits maxAllowedContentLength="4294967295" />
    <!-- bytes -->
  </requestFiltering>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With