Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS error when uploading larger files

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 ?

like image 205
Mitch Avatar asked Oct 28 '15 18:10

Mitch


1 Answers

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>
like image 53
acemp Avatar answered Sep 21 '22 10:09

acemp