1
我創建了應用程序使用離子和角度與php後端。從角度上傳和檢索圖像到php服務器
我可以根據下面的鍛鍊將角度上傳到php服務器的圖像。
Upload.js
$scope.onProfileFileSelect = function($files) {
//$files: an array of files selected, each file has name, size, and type.
for (var i = 0; i < $files.length; i++) {
var $file = $files[i];
console.log($file.name);
$upload.upload({
url: 'http://www.testphp.com/upload.php',
file: $file,
progress: function(e){}
}).then(function(response) {
// file is uploaded successfully
$timeout(function() {
console.log(response.data);
});
});
}
}
Upload.html
<div class="item item-input item-icon-right">
<span class="input-label">Capture your profile picture: </span>
<input type="file" name="ProfilePath" ng-model="inmateData.IMProfilePhotoPath" ng-file-select="onProfileFileSelect($files)" multiple />
</div>
upload.php的
<?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'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
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);
}
$target_dir = "Inmate_Images/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$result = move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
echo json_encode($_FILES["file"]);
echo($result);
?>
截至目前我能夠上傳選定的圖像並存儲在我的PHP服務器「Inmate_Images」文件夾中。
我的要求是:
我需要改變圖像文件名,然後直接上傳到服務器。
以及如何使用獲取請求獲取圖片以及上傳時提供的名稱。 ??
如何處理此活動的任何想法?
您是否嘗試過這兩點?什麼是錯誤?詢問具體情況。 – Jay
console.log($ file.name); - 我試圖通過$ .file.name =「userone」更改圖像名稱;但它沒有反映出來。我沒有任何想法訪問php文件夾。 –