Phonegap filetransfer在iPhone模拟器中工作但在设备上没有

时间:2012-05-09 10:58:04

标签: iphone cordova

以下位代码在我的iPhone模拟器中正常工作。我尝试使用Cordova 1.6的IOS 5.1,并尝试使用1.7。我的外部主机条目配置为127.0.0.1

这是代码:

          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 个答案:

答案 0 :(得分:1)

在设备上运行时,IP地址127.0.0.1或“localhost”是设备本身。这就是为什么你不打你的服务器。您必须输入托管上载脚本的服务器的主机名或IP地址。

此外,请确保将plist中的服务器列入白名单,否则将被拒绝。