AngularJs-“$http”工作,但“$resource”不工作-如何将“$http”参数应用于“$resource”`
我正在尝试将图像信息发布到后端。我尝试了AngularJs-“$http”工作,但“$resource”不工作-如何将“$http”参数应用于“$resource”`,angularjs,angular-resource,angular-http,Angularjs,Angular Resource,Angular Http,我正在尝试将图像信息发布到后端。我尝试了$resource,但没有成功。但是当我尝试使用$http时,它似乎是有效的。但是我已经在$http中添加了一些附加参数-但是我不知道如何将这些参数应用到$resource中有人能帮我吗 $scope.photoData = function (photoData) { console.log( "photo data", photoData ); //consoles properly var fileData =
$resource
,但没有成功。但是当我尝试使用$http
时,它似乎是有效的。但是我已经在$http
中添加了一些附加参数-但是我不知道如何将这些参数应用到$resource
中有人能帮我吗
$scope.photoData = function (photoData) {
console.log( "photo data", photoData ); //consoles properly
var fileData = photoData[0];
var data = new FormData();
var fileASHXPath = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';
//this is not working!
$resource(fileASHXPath).save({ "photoData" : fileData }).$promise.then( function ( response ) {
console.log( response ); //nothing works!
})
//this is works!
var base = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';
$http({
url: base + 'TestImage.jpg',
method: 'POST',
processData: false,
headers: { 'Content-Type': undefined },
data: fileData
}
)
.success(function (data) {
if (data.Message.toLowerCase() == 'success') {
console.log(data);
}
})
.error(function (msg, code) {
console.log('error method called');
});
}
更新-我的新尝试
仍不工作
$resource(base + fileName, {}, {
post:{
method:"POST",
isArray:false,
headers:{'Content-Type':'undefined'}
}
}).$promise.then(function ( response ) {
console.log( "response is", response );
})
尝试调用资源。即在$resource
定义之后,调用post()
试试这个。即在$resource定义之后,使用表单数据调用post()
$scope.photoData = function (photoData) {
console.log( "photo data", photoData ); //consoles properly
var fd = new FormData();
fd.append("photoData", photoData[0]);
var fileASHXPath = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';
$resource(fileASHXPath, {}, {
post: {
method: 'POST',
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}
}
).post({}, fd).$promise.then(function ( response ) {
console.log( "response is", response );
});
});
您可能忘了将其声明为控制器函数的参数。我不明白您的意思。。请进一步帮助我,如果可能,将您的建议发布给我据我所知,我们将通过声明为参数来获得$scope
,$http
和其他信息,对吗?在我的应用程序中,我得到了未定义的$window
,因为我没有在控制器声明的函数中使用该参数。(app.controller(“ctrlLogin”,函数($scope,$http,$location,$window){});
)。所以我想你可能忘了声明为参数。不,我已经声明了。一般情况下,如果我没有声明,我会得到错误。在我的情况下,我根本没有得到任何错误。从fd
我如何获得“图像名称”?(通常我使用photoData[0]。name
您无法获取图像名称,因为FormData不提供图像名称。它只是一个对象。您可以从任何后端语言(如PHP等)获取名称。。
$scope.photoData = function (photoData) {
console.log( "photo data", photoData ); //consoles properly
var fd = new FormData();
fd.append("photoData", photoData[0]);
var fileASHXPath = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';
$resource(fileASHXPath, {}, {
post: {
method: 'POST',
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}
}
).post({}, fd).$promise.then(function ( response ) {
console.log( "response is", response );
});
});