2016-09-14 73 views
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」文件夾中。

我的要求是:

  1. 我需要改變圖像文件名,然後直接上傳到服務器。

  2. 以及如何使用獲取請求獲取圖片以及上傳時提供的名稱。 ??

如何處理此活動的任何想法?

+0

您是否嘗試過這兩點?什麼是錯誤?詢問具體情況。 – Jay

+0

console.log($ file.name); - 我試圖通過$ .file.name =「userone」更改圖像名稱;但它沒有反映出來。我沒有任何想法訪問php文件夾。 –

回答

0

正如前面提到的我的要求第二點,我找到了答案,請找到下面的一個。任何建議請讓我知道。

preview.html

<img ng-src="http://www.testphp.com/Inmate_Images/mike.png" style="height:75px; padding: 5px 5px 5px 5px;"/> 

的事情是,你只是讓你的圖像的路徑在您的服務器文件夾,並直接使用< img>標籤,你可以能夠顯示。

但我的問題是如何改變圖像的文件名,同時從< INPUT TYPE =文件>標記選擇因爲我想更改文件名,而上傳它會更容易讓我再次得到文件。