2013-07-03 6 views
14

Projektu robię w Androidzie i chcę przesłać obraz na serwer.chcesz wgrać obrazek na serwer używając phonegapa w androidie

Ale nie mam pojęcia, gdzie powinienem umieścić ten kod.

Nie mogę pokazać żadnych przycisków do przesyłania zdjęć, proszę o pomoc.

Jestem nowy w tym. Sędziowałem ten kod z dokumentacji phonegap.

Próbuję tego przez wiele godzin, ale nie mogę uzyskać lepszego rozwiązania.

To jest mój pierwszy projekt phonegap na Androida.

Kod:

<head> 
    <script type="text/javascript" charset="utf-8" src="cordova-2.4.0.js"></script> 
    <script type="text/javascript" charset="utf-8">   
    document.addEventListener("deviceready", onDeviceReady, false); 

    function onDeviceReady() {   
     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 = {}; 
     params.value1 = "test"; 
     params.value2 = "param"; 

     options.params = params; 

     var ft = new FileTransfer(); 
     ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, options); 
    } 

    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); 
    } 

    </script> 
</head> 
<body> 
    <h1>Example</h1> 
    <p>Upload File</p> 
</body> 
+1

Błąd: Nie można odnaleźć zmienna: FileUploadOptions – nick

Odpowiedz

10

można rozwiązać problemu, korzystając z kolejnego kodu:

<script type="text/javascript"> 
function uploadFromGallery() { 

    // 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)+'.png'; 
    options.mimeType="text/plain"; 

    var params = new Object(); 

    options.params = params; 

    var ft = new FileTransfer(); 
    ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, options); 
} 

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); 
} 
</script> 
</head> 
<body> 
    <a data-role="button" onClick="uploadFromGallery();">Upload from Gallery</a> 
</body> 

Zobacz więcej informacji na temat tego postu: https://stackoverflow.com/a/13862151/1853864

+0

thankz alot it works – Manu

+2

Dzięki. Poprawnie się udało. Ale musimy przesłać wiele zdjęć. Proszę dać mi pomysł, aby przesłać wiele zdjęć za pomocą tego transferu pliku cordova. – Sathiyaraj

+0

Przepraszam, bro :(Nie mogę ci pomóc, ale myślę, że nie możesz przesłać wielu obrazów za pomocą transferu plików. Kiedy znalazłem to rozwiązanie, użyłem wtyczki i jakiegoś natywnego kodu na Androida i iOS. –

0

To jedna przykładowa aplikacja, która zrobiłem załadować pic na serwerze

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) 
{ 
    alert("imageURI: " + imageURI); 
    document.getElementById("myimg").src = imageURI; 
    alert("imageURI : " + 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(); 
    ft.upload(imageURI, "http://192.168.5.109:86/YourService.svc/SaveImage", win, fail, options); 
} 

function win(r) 
{ 
    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); 
} 

Nadzieja to pomaga

Dzięki AB

+0

plz proive mi kilka przycisków, aby wywołać funkcję wysyłania dis – Manu

+1

oprawę tej funkcji do jakiegoś przycisku! Stworzyć jeden ! ;) – MAST3RMIND

+0

ya zrobiłem to, ale nie wyświetlał się komunikat o błędzie ani niepowodzeniu – Manu

5

Spróbuj wykonać następujące czynności kod.

// A button will call this function 
// To capture photo 
function capturePhoto() { 
    // Take picture using device camera and retrieve image as base64-encoded string 
    navigator.camera.getPicture(uploadPhoto, onFail, { 
     quality: 50, destinationType: Camera.DestinationType.FILE_URI 
    }); 
} 

// A button will call this function 
// To select image from gallery 
function getPhoto(source) { 
    // Retrieve image file location from specified source 
    navigator.camera.getPicture(uploadPhoto, onFail, { quality: 50, 
     destinationType: navigator.camera.DestinationType.FILE_URI, 
     sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY 
    }); 
} 

function uploadPhoto(imageURI) { 
    //If you wish to display image on your page in app 
    // Get image handle 
    var largeImage = document.getElementById('largeImage'); 

    // Unhide image elements 
    largeImage.style.display = 'block'; 

    // Show the captured photo 
    // The inline CSS rules are used to resize the image 
    largeImage.src = imageURI; 

    var options = new FileUploadOptions(); 
    options.fileKey = "file"; 
    var userid = '123456'; 
    var imagefilename = userid + Number(new Date()) + ".jpg"; 
    options.fileName = imagefilename; 
    options.mimeType = "image/jpg"; 

    var params = new Object(); 
    params.imageURI = imageURI; 
    params.userid = sessionStorage.loginuserid; 
    options.params = params; 
    options.chunkedMode = false; 
    var ft = new FileTransfer(); 
    var url = "Your_Web_Service_URL"; 
    ft.upload(imageURI, url, win, fail, options, true); 
} 
//Success callback 
function win(r) { 
    alert("Image uploaded successfully!!"); 
} 
//Failure callback 
function fail(error) { 
    alert("There was an error uploading image"); 
} 
// Called if something bad happens. 
// 
function onFail(message) { 
    alert('Failed because: ' + message); 
} 

Utwórz przycisk na swojej stronie HTML, na to onclick wywołanie zdarzenia następujące funkcje, jak na swoje wymagania.

  • Zadzwoń pod capturePhoto() funkcję przechwytywania i przesyłania zdjęć.
  • Zadzwoń pod numer getPhoto(), aby pobrać obraz z galerii.

HTML powinien zawierać przyciski, takie jak:

<input name="button" type="button" onclick="capturePhoto()" value="Take Photo"/> 

<input name="button" type="button" onclick="getPhoto();" value="Browse" /> 

nadzieję, że pomoże.

+0

jak, gdzie i kiedy zadzwonić funkcja uploadPhoto (imageURI) i co będzie wartością parametru imageURI ??? –

+0

@ jolly.exe 'uploadPhoto()' get jest automatycznie wywoływany w callback 'capturePhoto()', a imageURI zostanie wysłane automatycznie. Nie musisz nigdzie tego wyraźnie mówić. –

+0

Mam również do czynienia z podobnym problemem, ale nie jestem w stanie go naprawić. możesz sprawdzić to pytanie stackoverflow.com/questions/40514847 – Ironic

0

dobrze, to praca dla mnie.

trustAllHosts: Optional parameter, defaults to false. If set to true, it accepts all security certificates. This is useful since Android rejects self-signed security certificates. Not recommended for production use. Supported on Android and iOS. (boolean)

Ostatni parametr dodaje true, jak boolean.

Przed

var ft = new FileTransfer(); 
ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail, options); 

Po

var ft = new FileTransfer(); 
ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail, options, true); 
Powiązane problemy