1

以下代码在我的 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

在我的服务器日志中,我可以看到设备根本没有连接。有什么我可能做错或应该检查的地方吗?

4

1 回答 1

1

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

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

于 2012-05-09T13:26:58.723 回答