简体   繁体   English

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

[英]Issues with uploading image to webserver using phonegap

EDIT: Changed win() function and added am image corresponding to it's result. 编辑:更改了win()函数,并添加了对应于其结果的图像。

I am having trouble uploading image to a webserver using phonegap. 我无法使用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(). 该代码触发win()函数,并在捕获图像时显示结果的JSON数据,因此至少不会失败()。

And here's an image of what win() function alerts. 这是win()函数警告的图像。

win()函数警报消息

and this is what my upload.php looks like: 这就是我的upload.php的样子:

<?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. 确保已将所有适当的(读/写)权限设置为目标文件夹uploads/位置,您尝试在其中上载文件。

Hope this helps!. 希望这可以帮助!。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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