2016-06-07 71 views
0

嘿傢伙我使用jQuery的,AJAX和HTML。這是我的上傳照片如何獲取圖像名稱上傳到服務器後在科爾多瓦

function uploadPhoto(imageURI) { 
     if (imageURI.substring(0,21)=="content://com.android") { 
      photo_split=imageURI.split("%3A"); 
      imageURI="content://media/external/images/media/"+photo_split[1]; 
     } 
     var options = new FileUploadOptions(); 
     options.fileKey="file"; 
     options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); 
     //alert(options.fileName); 
     options.mimeType="image/jpeg"; 

     var params = new Object(); 
     params.value1 = "test"; 
     params.value2 = "param"; 

     options.params = params; 
     options.chunkedMode = false; 

     var ft = new FileTransfer(); 
     ft.upload(imageURI, host+"/skripsitemplate/php/upload.php", win, fail, options); 
    } 

    function win(r) { 
     console.log("Code = " + r.responseCode); 
     console.log("Response = " + r.response); 
     console.log("Sent = " + r.bytesSent); 
    } 

此代碼工作正常。但我不知道如何獲取已經上傳到服務器的圖像名稱。圖像名稱將被上傳到數據庫。這是我upload.php的

<?php 
print_r($_FILES); 
$new_image_name = "foto".rand(1,1000)."_".date("Y-m-d-h-i-s").".jpg"; 
move_uploaded_file($_FILES["file"]["tmp_name"], "../web/uploads/".$new_image_name); 
echo $new_image_name; 
?> 

我真正關心科爾多瓦和PhoneGap的事情新手。謝謝你們,有一個愉快的一天

回答

2

根據你的PHP代碼,已上載被回覆回爲echo $new_image_name;的迴應,你可以在贏功能物體響應訪問的圖像的文件名:

function win(r) { 
    console.log("Code = " + r.responseCode); 
    console.log("Response = " + r.response); 
    console.log("Sent = " + r.bytesSent); 
    alert("filename is" + r.response); // <-- this is your filename 
} 

但是,您需要刪除$ _FILES對象上的print_r函數,因爲它將提供不需要的附加輸出。

<?php 
// print_r($_FILES); // <---- remove this 
$new_image_name = "foto".rand(1,1000)."_".date("Y-m-d-h-i-s").".jpg"; 
move_uploaded_file($_FILES["file"]["tmp_name"], "../web/uploads/".$new_image_name); 
echo $new_image_name; 
?> 
+0

非常感謝你@Shadi Shaaban – Rei