ng-file-upload 无法执行 setRequestHeader
ng-file-upload Failed to execute setRequestHeader
我遇到了 ng-file-upload 的问题,我使用的代码与 GitHub 中的示例几乎相同,但是当我尝试上传文件时出现此错误:
Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'function (xhr) {
if (!xhr || !(xhr instanceof XMLHttpRequest)) return;
config.__XHR = xhr;
if (config.xhrFn) config.xhrFn(xhr);
xhr.upload.addEventListener('progress', function (e) {
e.config = config;
notifyProgress(getNotifyEvent(e));
}, false);
//fix for firefox not firing upload progress end, also IE8-9
xhr.upload.addEventListener('load', function (e) {
if (e.lengthComputable) {
e.config = config;
notifyProgress(getNotifyEvent(e));
}
}, false);
}' is not a valid HTTP header field value.
我正在使用最新版本的 ng-file-upload 和 angular 1.5.0(但也用 1.4.7 测试过,同样的错误)。我只是复制了这个例子,所以我的 HTML of directive with upload 看起来像:
<div>
<label>{{field.label}} <span ng-if="field.required" class="text-red">*</span></label>
<p ng-show="field.help">
{{field.help}}
</p>
Drop File:
<div ngf-drop ngf-select ng-model="files" class="drop-box"
ngf-drag-over-class="'dragover'" ngf-multiple="true" ngf-allow-dir="true"
accept="image/*,application/pdf"
ngf-pattern="'image/*,application/pdf'">Drop pdfs or images here or click to upload</div>
<div ngf-no-file-drop>File Drag/Drop is not supported for this browser</div>
Files:
<ul>
<li ng-repeat="f in files">{{f.name}} {{f.$error}} {{f.$errorParam}}</li>
</ul>
Upload Log:
<pre>{{log}}</pre>
</div>
我的上传服务:
scope.$watch('files', function() {
scope.upload(scope.files);
});
scope.log = '';
scope.upload = function(files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (!file.$error) {
Upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
data: {
file: file
}
}).progress(function(evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
scope.log = 'progress: ' + progressPercentage + '% ' +
evt.config.data.file.name + '\n' + scope.log;
}).success(function(data, status, headers, config) {
$timeout(function() {
scope.log = 'file: ' + config.data.file.name + ', Response: ' + JSON.stringify(data) + '\n' + scope.log;
});
});
}
}
}
};
我在 angular.js 之前添加了垫片:
<script type="text/javascript" src="vendor/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="vendor/datatables/media/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="vendor/ng-file-upload/ng-file-upload-shim.min.js"></script>
<script type="text/javascript" src="vendor/angular/angular.js"></script>
...
<script type="text/javascript" src="vendor/ng-file-upload/ng-file-upload.js"></script>
<script type="text/javascript" src="src/app/app.js"></script>
<script type="text/javascript" src="src/app/client/client.js"></script>
知道哪里出了问题吗?
好的,问题是由 PACE.js 库(XHR 请求的 JS 进度条)引起的,解决方案是删除它,或者应用此补丁 - https://github.com/danialfarid/ng-file-upload/issues/98
将此行添加到上传服务它将起作用
我遇到了 ng-file-upload 的问题,我使用的代码与 GitHub 中的示例几乎相同,但是当我尝试上传文件时出现此错误:
Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'function (xhr) {
if (!xhr || !(xhr instanceof XMLHttpRequest)) return;
config.__XHR = xhr;
if (config.xhrFn) config.xhrFn(xhr);
xhr.upload.addEventListener('progress', function (e) {
e.config = config;
notifyProgress(getNotifyEvent(e));
}, false);
//fix for firefox not firing upload progress end, also IE8-9
xhr.upload.addEventListener('load', function (e) {
if (e.lengthComputable) {
e.config = config;
notifyProgress(getNotifyEvent(e));
}
}, false);
}' is not a valid HTTP header field value.
我正在使用最新版本的 ng-file-upload 和 angular 1.5.0(但也用 1.4.7 测试过,同样的错误)。我只是复制了这个例子,所以我的 HTML of directive with upload 看起来像:
<div>
<label>{{field.label}} <span ng-if="field.required" class="text-red">*</span></label>
<p ng-show="field.help">
{{field.help}}
</p>
Drop File:
<div ngf-drop ngf-select ng-model="files" class="drop-box"
ngf-drag-over-class="'dragover'" ngf-multiple="true" ngf-allow-dir="true"
accept="image/*,application/pdf"
ngf-pattern="'image/*,application/pdf'">Drop pdfs or images here or click to upload</div>
<div ngf-no-file-drop>File Drag/Drop is not supported for this browser</div>
Files:
<ul>
<li ng-repeat="f in files">{{f.name}} {{f.$error}} {{f.$errorParam}}</li>
</ul>
Upload Log:
<pre>{{log}}</pre>
</div>
我的上传服务:
scope.$watch('files', function() {
scope.upload(scope.files);
});
scope.log = '';
scope.upload = function(files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (!file.$error) {
Upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
data: {
file: file
}
}).progress(function(evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
scope.log = 'progress: ' + progressPercentage + '% ' +
evt.config.data.file.name + '\n' + scope.log;
}).success(function(data, status, headers, config) {
$timeout(function() {
scope.log = 'file: ' + config.data.file.name + ', Response: ' + JSON.stringify(data) + '\n' + scope.log;
});
});
}
}
}
};
我在 angular.js 之前添加了垫片:
<script type="text/javascript" src="vendor/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="vendor/datatables/media/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="vendor/ng-file-upload/ng-file-upload-shim.min.js"></script>
<script type="text/javascript" src="vendor/angular/angular.js"></script>
...
<script type="text/javascript" src="vendor/ng-file-upload/ng-file-upload.js"></script>
<script type="text/javascript" src="src/app/app.js"></script>
<script type="text/javascript" src="src/app/client/client.js"></script>
知道哪里出了问题吗?
好的,问题是由 PACE.js 库(XHR 请求的 JS 进度条)引起的,解决方案是删除它,或者应用此补丁 - https://github.com/danialfarid/ng-file-upload/issues/98
将此行添加到上传服务它将起作用