我正在使用離子框架構建移動應用程序。我需要讓用戶從他們的手機庫中選擇一個圖像(使用Cordova ImagePicker)並點擊上傳按鈕。一旦用戶點擊上傳按鈕,圖像和用戶名必須傳遞到服務器,然後服務器處理圖像和用戶名,並使用PHPMailer類將其郵寄到指定的電子郵件ID。到目前爲止,我成功地將用戶名發送到服務器,併成功將其轉發爲電子郵件,但我不知道如何隨圖像一起發送圖像。從離子應用程序發佈圖像到PHP服務器
請幫忙!
注意:我不想將圖像上傳到服務器,或者我可以使用Cordova File Transfer插件。我只是想將圖像傳遞給PHPMailer類併發送郵件。
客戶端
.controller('UploadCtrl', function($scope,$cordovaImagePicker,$http) {
$scope.collection = {
selectedImage : ''
};
$scope.takePicture = function() {
var options = {
maximumImagesCount:1,
width: 800,
height: 800,
quality: 80
};
$cordovaImagePicker.getPictures(options).then(function (results) {
for (var i = 0; i < results.length; i++) {
$scope.collection.selectedImage = results[i];
window.plugins.Base64.encodeFile($scope.collection.selectedImage, function(base64){
$scope.collection.selectedImage = base64;
});
}
}, function(error) {
console.log('Error: ' + JSON.stringify(error));
});
};
$scope.data = {};
$scope.upload = function() {
var link = 'http://example.com/upload.php';
$http.post(link, {username : "xyz"}).then(function (res){
$scope.response = res.data;
console.log($scope.response);
});
};
})
服務器端
<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers:
{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$postdata = file_get_contents("php://input");
if (isset($postdata)) {
$request = json_decode($postdata);
$username = $request->username;
if ($username != "") {
echo "Server returns: " . $username;
require("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "[email protected]"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = "[email protected]";
$mail->FromName = "Example";
$mail->AddAddress("[email protected]", "ABC");
$mail->WordWrap = 50;
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
$mail->IsHTML(true);
$mail->Subject = "New List Uploaded";
$mail->Body = $username;
$mail->AltBody = "Alternate Body";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
}
else {
echo "Empty username parameter!";
}
}
else {
echo "Not called properly with username parameter!";
}
?>
你好,所以你正面臨問題上傳圖像在服務器上? –
嘿!那種。我如何將$ scope.collection.selectedImage傳遞給服務器?我不想將圖像上傳到服務器。我只是想將圖像傳遞給PHPMailer類併發送郵件。 – vkm
我完全不知道如何做到這一點。一個描述性的說明會有所幫助。 – vkm