我正在研究離子聊天應用程序,用戶可以將照片作爲其消息的一部分上載。我正在尋找一種方法將圖像上傳到我的虛擬主機服務器,以便以後通過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,那麼該怎麼辦? –