2012-03-26 81 views
1

我正在嘗試創建一個非常基本的照片上傳功能。我已經在線學習了很多教程和示例,但在實際使用中遇到了一些問題。Phonegap:照片不上傳

目前我收到以下錯誤:

2012-03-26 17:37:02.107 Upload[84710:13403] fileData length: 72154 
2012-03-26 17:37:02.119 Upload[84710:13403] File Transfer Error: unsupported URL 
2012-03-26 17:37:02.121 Upload[84710:13403] [INFO] Error in error callback: org.apache.cordova.filetransfer1 = ReferenceError: Left side of assignment is not a reference. 

這是我的PhoneGap HTML文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
    <title>File Transfer Example</title> 
    <script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script> 
    <script type="text/javascript" charset="utf-8"> 

     // Wait for PhoneGap to load 
     document.addEventListener("deviceready", onDeviceReady, false); 

     // PhoneGap is ready 
     function onDeviceReady() { 
    // Do cool things here... 
     } 

     function getImage() { 
      // Retrieve image file location from specified source 
      navigator.camera.getPicture(uploadPhoto, function(message) { 
alert('get picture failed'); 
},{ 
quality: 50, 
destinationType: navigator.camera.DestinationType.FILE_URI, 
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY 
} 
      ); 

     } 

     function uploadPhoto(imageURI) { 
      var options = new FileUploadOptions(); 
      options.fileKey="file"; 
      options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); 
      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, "hosting.domain.co.uk/ios/upload.php", win, fail, options); 
     } 

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

     function fail(error) { 
      alert("An error has occurred: Code = " = error.code); 
     } 

     </script> 
</head> 
<body> 
    <button onclick="getImage();">Upload a Photo</button> 
</body> 
</html> 

這是我upload.php的文件:

<?php 
print_r($_FILES); 
$new_image_name = "namethisimage.jpg"; 
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/vhosts/domain.co.uk/subdomains/hosting/httpdocs/ios/uploads".$new_image_name); 
?> 

有任何明顯的我做錯了?

非常感謝

喬恩

+1

您是否列出了您的域名? – 2012-03-26 16:55:59

+0

是的,我已啓用*爲白名單進行測試 – user1293545 2012-03-27 08:57:29

+0

您的網址無效:hosting.domain.co.uk/ios/upload.php。嘗試在前面添加http://。 – 2012-03-27 10:52:02

回答

4

您使用了fileURI上傳照片,這就是爲什麼你所得到的不支持的文件格式錯誤。這是因爲fileURI格式爲file://localhost/xx1.jpg。這將不被服務器端的上傳器功能支持。

JavaScript不能訪問本地文件系統

JavaScript沒有直接訪問底層的文件系統,因此它不能在PhoneGap的情況下通過文件傳輸上傳。

解決方案

你最好的選擇是不是上傳圖像編碼字符串中的Base64。您可以使用navigator.camera.DestinationType.DATA_URLdestinationType獲得base64編碼圖像。這將返回base64編碼的字符串。這可以很容易地通過JSON發送到服務器與相關的元數據,如JPG或PNG格式和文件名等。

看看here成功使用phonegap和imgur提供的在線圖像託管設施。

祝您好運,歡呼

+0

使用DestinationType.DATA_URL不被phonegap建議(有內存問題),如下所示: http://docs.phonegap.com/zh/2.7.0/cordova_camera_camera.md.html#camera.getPicture – 2013-05-10 01:26:54

+0

Great解釋!謝謝 – 2015-04-09 07:19:56