上传较大文件时出现CORS错误 [英] CORS error when uploading larger files

查看:1048
本文介绍了上传较大文件时出现CORS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个启用了CORS的c#web api的Angular Web项目.

I am using an angular web project with c# web api with CORS enabled.

除了将文件上传到异步任务时,所有的CORS均可在所有呼叫上正常工作.这是我引用的方法.

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
}

我得到的错误是:

请求的资源上没有"Access-Control-Allow-Origin"标头.

No 'Access-Control-Allow-Origin' header is present on the requested resource.

因此,显然,我什至不被允许调用我的UploadFile方法.

So obviously I am not even allowed to call my UploadFile method.

在小文件上,该呼叫有效,并且响应中显示"Access-Control-Allow-Origin",但在60 mb +的大文件上,则不存在.

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.

我正在使用XMLHttpRequest进行网络api调用.

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);

跨源资源共享请求是否有可能在通过xhr.send发送文件之前超时?

Is it possible for the Cross Origin Resource Sharing request to timeout before the file is sent throught the xhr.send ?

推荐答案

请确保将web.config中的最大允许内容长度"设置为足够高,否则会出现错误.检查您的IIS日志以查看是否存在此问题.

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>

这篇关于上传较大文件时出现CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆