我正在使用visual studio 2015 rc的Apache cordova模板和离子框架开发跨平台移动应用程序.App的主要功能是将图像从相机上传到远程主机。 为此,我在角度控制器中使用文件传输插件,在服务器端使用RestApi(ASP.net webapi)。
但在测试时不断给我这个错误。
例外:
错误: { "代码":1,"源":" API /文件上传/ uploadfile""目标":" API /文件上传/ uploadfile"" HTTP_STATUS":空,"主体":空,"例外" {"描述":访问 被拒绝" ...}
这是我的控制器代码
.controller("ImageUploadCtrl", function($scope, $cordovaFileTransfer) {
$scope.upload = function() {
var options = {
fileKey: "file",
fileName: "image.jpg",
chunkedMode: false,
mimeType: "image/jpeg"
uploadOptions.httpMethod = "POST";
uploadOptions.headers = {
Connection:"close"
};
};
$cordovaFileTransfer.upload("api/fileupload/uploadfile", "/images/image.jpg", options).then(function (result) {
console.log("SUCCESS: " + JSON.stringify(result.response));
}, function (err) {
console.log("ERROR: " + JSON.stringify(err));
}, function (progress) {
// constant progress updates
});
}
})
这是我的Api代码
public void UploadFile()
{
if (HttpContext.Current.Request.Files.AllKeys.Any())
{
// Get the uploaded image from the Files collection
var httpPostedFile = HttpContext.Current.Request.Files["file"];
if (httpPostedFile != null)
{
// Validate the uploaded image(optional)
// Get the complete file path
var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), httpPostedFile.FileName);
// Save the uploaded file to "UploadedFiles" folder
httpPostedFile.SaveAs(fileSavePath);
}
}
}
需要帮助。欢迎提出任何建议。 此致
答案 0 :(得分:2)
我为公司构建的应用程序有同样的问题,我们无法让文件加载器工作,我们做的是我们刚刚将图像作为base64字符串发布到我们的服务器。然后,您可以简单地从数据库中提取字符串并将其显示在div中。我们使用NgCordova相机,然后只传入takePhoto功能的数据。在android(而不是ios)设备上,您可以使用lz-string.js压缩字符串。同样在Ios上我们必须制作图像png。如果您的应用只需要处理上传图片,这是一种简单易用的方法。
$scope.takePhoto = function () {
$ionicScrollDelegate.scrollTop();
console.log('fired camera');
$scope.uploadList = false;
$ionicPlatform.ready(function() {
var options = {
quality: 100,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: false,
encodingType: Camera.EncodingType.PNG,
targetWidth: 800,
targetHeight: 1100,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
};
$cordovaCamera.getPicture(options).then(function (imageData) {
$ionicLoading.show({
template: 'Processing Image',
duration: 2000
});
$scope.image = "data:image/png;base64," + imageData;
if (ionic.Platform.isAndroid() === true) {
$scope.Data.Image = LZString.compressToUTF16($scope.image);
$scope.Data.isCompressed = 1;
} else {
$scope.Data.Image = $scope.image;
$scope.Data.isCompressed = 0;
}
if ($scope.tutorial) {
$scope.showAlert("Instructions: Step 3", '<div class="center">Now that you have taken a photo of the POD form, you must upload it to the server. Press the upload doc button in the bottom right of the screen.</div>');
}
$scope.on('')
}, function (err) {
console.log(err);
});
}, false);
};
$scope.UploadDoc = function () {
var req = {
method: 'POST',
url: ffService.baseUrlAuth + 'cc/upload',
headers: {
'x-access-token': ffService.token
},
data: $scope.Data
};
if ($scope.Data.Image === null || $scope.Data.Value === '') {
$scope.showAlert("Uh Oh!", '<div class="center">Please take a photo of your document before attempting an upload.</div>');
} else {
$http(req).success(function (data, status, headers, config) {
localStorage.setItem('tutorial', false);
$scope.tutorial = false;
$scope.getUploads($scope.PODOrder.OrderNo);
$scope.showAlert("Success!", '<div class="center">Your Document has been successfully uploaded!</div>');
$scope.uploadList = true;
}).error(function (data, status, headers, config) {
$rootScope.$broadcast('loading:hide');
$scope.showAlert("Something went wrong!", '<div class="center">Please make sure you have an internet connection and try again.</div>');
}).then(function(data, status, headers, config){
$scope.Data.Image = null;
});
}
};
答案 1 :(得分:2)
最后这个简单的解决方案对我有用。
<强>更新强> 在App控制器和Webapi控制器中更改代码之后 每件像魅力和图像一样的东西都是通过API开始在服务器上保存的。
更改代码 我在App控制器选项中添加这些行:
uploadOptions.httpMethod = "POST";
uploadOptions.headers = {
Connection:"close"
};
并在WebApi Controller中更改此行
var httpPostedFile = HttpContext.Current.Request.Files["file"];
我还在API中启用了CORS。
感谢。