2012-05-09 157 views
1

以下幾段代碼在我的iPhone模擬器中正常工作。我嘗試使用Cordova 1.6的IOS 5.1,並嘗試1.7。我的外部主機條目被配置爲127.0.0.1Phonegap filetransfer在iPhone模擬器中工作,但不在設備上

這是代碼:

  function uploadPhoto(imageElement){ 
       var options = new FileUploadOptions(); 
       var options = new FileUploadOptions(); 
       options.fileKey="file"; 
       options.mimeType="image/jpeg"; 
       options.fileName="carimage.jpg"; 
       options.chunkedMode = true; 
       var params = new Object(); 
       params.value1 = "Car"; 
       params.value2 = "Used"; 

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

       var ft = new FileTransfer(); 
       console.log("Upload Photo"); 
       ft.upload(
         document.getElementById(imageElement).src, 
         "http://127.0.0.1:8080/claim/image", 
         win, 
         fail, 
         options, 
         true); 
       console.log("Done Uploading"); 
      } 

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

      function fail(error) { 
       alert("An error has occurred: Code = " + error.code); 
       console.log("upload error source " + error.source); 
       console.log("upload error target " + error.target); 
      } 

在Xcode模擬器輸出是:

2012-05-09 12:45:09.390 motorazzi[24964:13403] Multi-tasking -> Device: YES, App: YES 
2012-05-09 12:48:28.421 motorazzi[24964:13403] [INFO] Upload Photo 
2012-05-09 12:48:28.484 motorazzi[24964:13403] [INFO] Done Uploading 
2012-05-09 12:48:40.662 motorazzi[24964:13403] [INFO] Code = 200 
2012-05-09 12:48:40.662 motorazzi[24964:13403] [INFO] Response = 
2012-05-09 12:48:40.663 motorazzi[24964:13403] [INFO] Sent = 1358 

這很好,但使用從Xcode中設備,我得到:

2012-05-09 12:51:40.537 motorazzi[885:707] Multi-tasking -> Device: YES, App: YES 
2012-05-09 12:51:56.554 motorazzi[885:707] [INFO] Upload Photo 
2012-05-09 12:51:56.654 motorazzi[885:707] [INFO] Done Uploading 
2012-05-09 12:51:56.748 motorazzi[885:707] File Transfer Error: Could not connect to the server. 
2012-05-09 12:51:56.798 motorazzi[885:707] [INFO] An error has occurred: Code = 3 
2012-05-09 12:51:56.803 motorazzi[885:707] [INFO] upload error source http://127.0.0.1:8080/claim/image 
2012-05-09 12:51:56.813 motorazzi[885:707] [INFO] upload error target file:///var/mobile/Applications/699481A7-C598-43BA-BDCA-962DEF925EE5/tmp/photo_020.jpg 

在我的服務器日誌上,我可以看到設備根本沒有連接。有什麼我可能做錯了,或應該檢查?

回答

1

當您在設備上運行時,IP地址127.0.0.1或「localhost」是設備本身。這就是爲什麼你沒有打你的服務器。您將必須輸入托管上傳腳本的服務器的主機名或IP地址。

此外,請確保您將您的plist中的服務器列入白名單,否則它將被拒絕。

相關問題