Angularjs 带ng文件上传的角度文件上传
我正在上载图像文件,用于图像上载。使用给出的示例,我遇到了访问控制头错误Angularjs 带ng文件上传的角度文件上传,angularjs,ng-file-upload,Angularjs,Ng File Upload,我正在上载图像文件,用于图像上载。使用给出的示例,我遇到了访问控制头错误 vm.uploadPic = function(file) { file.upload = Upload.upload({ url: 'http://localhost:8000/api/v1/quotes/quoteitem/image/upload', data: {quote_item_id: vm.quote_item_id, filename: file} })
vm.uploadPic = function(file) {
file.upload = Upload.upload({
url: 'http://localhost:8000/api/v1/quotes/quoteitem/image/upload',
data: {quote_item_id: vm.quote_item_id, filename: file}
});
}
这就产生了错误
无法加载XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”
我不需要任何标题请求,而上传图像在邮递员,所以,我删除了标题
vm.uploadPic = function(file) {
file.upload = Upload.upload({
url: domain+'/api/v1/quotes/quoteitem/image/upload',
data: {quote_item_id: vm.quote_item_id, filename: file},
transformRequest: function(data, headersGetter) {
var headers = headersGetter();
headers['Content-type']=undefined;
return headers;
}
});
}
这给
TypeError:data.hasOwnProperty不是函数
at ng file upload.js:310
在angular.js:10484
在forEach(angular.js:322)
在transformData(angular.js:10483)
在$get.serverRequest(angular.js:11211)
在processQueue(angular.js:15961)
在angular.js:15977
范围$get.Scope.$eval(angular.js:17229)
范围$get.Scope.$digest(angular.js:17045)
在作用域$get.Scope.$apply(angular.js:17337)
我已经在这里面呆了很长时间了。我在服务器端进行了测试,它在postman中运行良好。任何帮助都会很好。问题是您正在从站点的端口3000上载到端口8000的端点。它们被认为是独立的来源,因此浏览器的安全功能正在发挥作用。您需要将它们放在同一个源上,或者将“Access Control Allow origin”头添加到上载终结点的服务器端响应。问题在于您正在从站点的端口3000上载到端口8000的终结点。它们被认为是独立的来源,因此浏览器的安全功能正在发挥作用。您需要将它们放在同一个源上,或者将“Access Control Allow origin”头添加到上载端点的服务器端响应中。请尝试这些方法
myApp.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(data){
alert("success");
})
.error(function(data){
alert("error");
});
};
}]);
myApp.controller('fupController', ['$scope', 'fileUpload', '$http', function($scope, fileUpload, $http){
$scope.uploadFile = function(){
var file = $scope.myFile;
console.log('file is '+ file );
console.dir(file);
var uploadUrl = 'http://localhost:8000/api/v1/quotes/quoteitem/image/upload';
fileUpload.uploadFileToUrl(file, uploadUrl);
};
}]);
请试试这个
myApp.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(data){
alert("success");
})
.error(function(data){
alert("error");
});
};
}]);
myApp.controller('fupController', ['$scope', 'fileUpload', '$http', function($scope, fileUpload, $http){
$scope.uploadFile = function(){
var file = $scope.myFile;
console.log('file is '+ file );
console.dir(file);
var uploadUrl = 'http://localhost:8000/api/v1/quotes/quoteitem/image/upload';
fileUpload.uploadFileToUrl(file, uploadUrl);
};
}]);
试试这个
<form method="post" enctype="multipart/form-data" ng-controller="commentCtrl" name="form">
<a href="" type="file" class="custom-height"><img src="source/assets/images/icons/icofileattached.png" class="attachmentpng-height" ngf-select="uploadFiles($file)" ng-model="files"/></a>
<md-button type="submit" class="md-raised custom-submit-button" ng-click="MakeComments()"> SUBMIT </md-button>
</form>
$scope.uploadFiles = function(file) {
console.log(file);
$scope.fileData = file;
var fd = new FormData();
fd.append('file', file);
Restangular.one('/api/files/end points').withHttpConfig({transformRequest: angular.identity})
.customPOST(fd, '', undefined, {'Content-Type': undefined})
};
提交
$scope.uploadFiles=函数(文件){
console.log(文件);
$scope.fileData=文件;
var fd=新FormData();
fd.append('file',file);
restanglar.one('/api/files/endpoints')。withHttpConfig({transformRequest:angular.identity})
.customPOST(fd',未定义,{'Content-Type':未定义})
};
试试这个
<form method="post" enctype="multipart/form-data" ng-controller="commentCtrl" name="form">
<a href="" type="file" class="custom-height"><img src="source/assets/images/icons/icofileattached.png" class="attachmentpng-height" ngf-select="uploadFiles($file)" ng-model="files"/></a>
<md-button type="submit" class="md-raised custom-submit-button" ng-click="MakeComments()"> SUBMIT </md-button>
</form>
$scope.uploadFiles = function(file) {
console.log(file);
$scope.fileData = file;
var fd = new FormData();
fd.append('file', file);
Restangular.one('/api/files/end points').withHttpConfig({transformRequest: angular.identity})
.customPOST(fd, '', undefined, {'Content-Type': undefined})
};
提交
$scope.uploadFiles=函数(文件){
console.log(文件);
$scope.fileData=文件;
var fd=新FormData();
fd.append('file',file);
restanglar.one('/api/files/endpoints')。withHttpConfig({transformRequest:angular.identity})
.customPOST(fd',未定义,{'Content-Type':未定义})
};
我已经在服务器端完成了cors设置。所有其他路由都可以访问,但只有此路由是不允许的。如果可以,请尝试将它们设置为在同一端口上运行。如果它能工作,那将证明你的Angular代码没有问题。我已经在服务器端完成了cors设置。所有其他路由都可以访问,但只有此路由是不允许的。如果可以,请尝试将它们设置为在同一端口上运行。如果有效,这将证明您的Angular代码没有问题。服务器不会接收任何文件。如果您知道如何将图像存储到本地存储,服务器不会接收任何文件。如果您知道如何将图像存储到本地存储,您的服务器需要允许CORS进行POST和选项。错误信息很清楚。从浏览器的“网络”选项卡查看来自服务器的响应内容,并确保返回的选项请求包含正确的标题。服务器需要允许CORS发布和选项。错误信息很清楚。从浏览器的“网络”选项卡查看来自服务器的响应内容,并确保返回的选项请求具有正确的标题。