使用phonegap将图像上传到网络服务器的问题

时间:2016-08-22 07:18:49

标签: javascript php cordova file-upload

编辑:更改了win()函数并添加了与其结果相对应的图像。

我无法使用phonegap将图片上传到网络服务器。

这是我对该应用的代码:

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

代码触发win()函数并在捕获图像时显示结果的JSON数据,因此至少它不会失败()。

这是win()函数警报的图像。

win() function alert message

这就是我的upload.php的样子:

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

1 个答案:

答案 0 :(得分:1)

确保您已将所有相应的(读/写)权限设置为尝试上传文件的目标文件夹uploads/

希望这有帮助!