简体   繁体   中英

Issues with uploading image to webserver using phonegap

EDIT: Changed win() function and added am image corresponding to it's result.

I am having trouble uploading image to a webserver using phonegap.

This is the code that I have for the app:

var pictureSource;   // picture source
var destinationType; // sets the format of returned value

// Wait for Cordova to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);

// Cordova is ready to be used!
//
function onDeviceReady() {
    pictureSource=navigator.camera.PictureSourceType;
    destinationType=navigator.camera.DestinationType;
}

// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {

  // 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";
  options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
  options.mimeType="image/jpeg";

  var params = new Object();
  params.value1 = "test";
  params.value2 = "param";

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

  var ft = new FileTransfer();
  ft.upload(imageURI, "http://www.tayabsoomro.me/upload.php", win, fail, options);
}

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

function fail(error){
  alert("An error occured while uploading image: " + error.code);
}

The code triggers win() function and shows the JSON data of the result when the image is captured so at least it doesn't fail().

And here's an image of what win() function alerts.

win()函数警报消息

and this is what my upload.php looks like:

<?php
print_r($_FILES);
$new_image_name = "myimg.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/".$new_image_name);
?>

Ensure that you have all the appropriate (Read/Write) permissions set to your target folder uploads/ where you try to upload your files.

Hope this helps!.

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