我正在研究离子聊天应用程序,用户可以将照片作为其消息的一部分上载。我正在寻找一种方法将图像上传到我的虚拟主机服务器,以便以后通过URL检索。从相机/照片库上载离子应用程序图像
问题是我无法将它上传到我的网络服务器。
我使用这两个插件:
- org.apache.cordova.file转移
- 科尔多瓦 - 插件相机
当我运行在Xcode模拟器应用程序和从设备光盘库中选择一张照片,控制台给我以下信息:
File Transfer Finished with response code 200
void SendDelegateMessage(NSInvocation *): delegate (webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode>
SUCCESS: ""
这是我目前使用的代码:
app.controller('HomeController', function($rootScope, $scope, $cordovaCamera, $ionicActionSheet, $cordovaFileTransfer){ ...
// open PhotoLibrary
$scope.openPhotoLibrary = function() {
var options = {
quality: 100,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
};
$cordovaCamera.getPicture(options).then(function(imageData) {
//console.log(imageData);
//console.log(options);
var url = "http://mydomein.com/upload.php";
//target path may be local or url
var targetPath = imageData;
var filename = targetPath.split("/").pop();
var options = {
fileKey: "file",
fileName: filename,
chunkedMode: false,
mimeType: "image/jpg"
};
$cordovaFileTransfer.upload(url, targetPath, options).then(function(result) {
console.log("SUCCESS: " + JSON.stringify(result.response));
alert("success");
alert(JSON.stringify(result.response));
}, function(err) {
console.log("ERROR: " + JSON.stringify(err));
alert(JSON.stringify(err));
}, function (progress) {
// constant progress updates
$timeout(function() {
$scope.downloadProgress = (progress.loaded/progress.total) * 100;
})
});
}, function(err) {
// error
console.log(err);
});
}
这是我的upload.php的文件:
<?php
// move_uploaded_file($_FILES["file"]["tmp_name"], $cwd . '/files/images/');
move_uploaded_file($_FILES["file"]["tmp_name"], "/files/images");
?>
如果您使用其他服务器,如Node.js和Express.js,那么该怎么办? –