简体   繁体   中英

How to upload the image to server in android phonegap

I have tried to upload image using FileTransfer.upload in phonegap api.But I am getting the error.The error is "Connection Error" and the error code is 3.

Here is my code.
<!DOCTYPE html>
<html>
<head>
<title>Video Uploader</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="master.css" type="text/css" />
<script type="text/javascript" charset="utf-8"src="phonegap-1.1.0.js"></script>
<script src="Scripts/jQuery-1.6.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>

<script type="text/javascript" charset="utf-8">
 function getphoto(){
   navigator.camera.getPicture(uploadPhoto,function(message) { 
   alert('get picture failed');
   },{   quality:10,destinationType:navigator.camera.DestinationType.FILE_URI,sourceType:navigator.camera.PictureSourceType.PHOTOLIBRARY }); 
   }

     function uploadPhoto(imageURI) {   

        document.getElementById("myimg").src=imageURI;

        var options = new FileUploadOptions(); 
        options.chunkedMode  = false;
        options.fileKey="recFile"; 
        var imagefilename = imageURI; 
        options.fileName=imagefilename; 
        options.mimeType="image/jpeg"; 


        var ft = new FileTransfer(); 
        alert(imagefilename);


        ft.upload(imageURI, "http://xxxx.asmx", win, fail, options,true); 
    } 
     function win(r) { 

        alert("Response = " + r.response); 
        alert("Sent = " + r.bytesSent); 
    } 
    function fail(error) { 
     switch (error.code) { 
                case FileTransferError.FILE_NOT_FOUND_ERR: 
                    alert("Photo file not found"); 
                    break; 
                case FileTransferError.INVALID_URL_ERR: 
                    alert("Bad Photo URL"); 
                    break; 
                case FileTransferError.CONNECTION_ERR: 
                    alert("Connection error"); 
                    break; 
            } 

        alert("An error has occurred: Code = " + error.code); 
    } 
   </script>
  </head>
   <body >
   <button onclick="getphoto();">get a Photo</button>
   <button onclick="getphoto();">Upload a Photo</button>
   <img src="" id="myimg" style="border:1px solid #0f0;height:200px;width:200px;" />

   </body>
  </html>

please see that and tell me the solution.What is the concept of upload.php in phonegap api

Have you tried to upload to another server? If the upload succeeds, the problem can be in your server settings. I'm having this trouble and starting to look to my nginx/passenger conf.

EDIT :

I solved this by increasing client_max_body_size setting on nginx (it defaults to 1MB).

Also, it seems that your last parameter on "ft.upload" (the "true") is meant for https requests, so you should omit that in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM