作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用启用了 CORS 的 c# web api 的 Angular web 项目。
除了我将文件上传到异步任务时,我的所有 CORS 在所有调用中都能正常工作。这是我所指的方法。
[HttpPost]
public async Task<HttpResponseMessage> UploadFile(){
//code
}
我得到的错误是:
请求的资源上不存在“Access-Control-Allow-Origin” header 。
很明显,我什至不允许调用我的 UploadFile 方法。
在小文件上调用有效,响应中出现“Access-Control-Allow-Origin”,但在 60 MB 以上的大文件上则无效。
我正在使用 XMLHttpRequest 进行网络 API 调用。
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 发送之前超时?
最佳答案
确保 web.config 中的 Max allowed content Length 设置得足够高,否则会出现错误。检查您的 IIS 日志,看看是否是问题所在。
<requestFiltering>
<requestLimits maxAllowedContentLength="4294967295" />
<!-- bytes -->
</requestFiltering>
关于angularjs - 上传较大文件时出现 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33399267/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!