我正在尝试运行此PhoneGap 示例以将图像从设备上传到服务器。
// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
// 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);
options.mimeType="image/jpeg";
var params = new Object();
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, "http://some.server.com/upload.php", win, fail, options);
}
function win(r) {
alert("Code = " + r.responseCode);
alert("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
alert("upload error source " + error.source);
alert("upload error target " + error.target);
}
就我而言,与上面提到的示例不同,我没有使用将数据发布到数据库的 php 文件,而是使用安全 url 直接通过 HTML 发布数据。在桌面版本上看起来像这样的东西:
<form action="https://mySecureUrl.com/?filename=myImage" method="post" enctype="multipart/form-data">
<input type="file" name="myfile"><br>
<input type="submit" value="Upload File to Server">
</form>
现在我正在尝试使用 PhoneGap FileTransfer() 应用相同的方法,我得到错误代码 3。
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("https://mySecureUrl.com/?filename=myImage.jpg"), win, fail, options);
- 是否可以在 PhoneGap 上以这种方法发布文件?如何?
- 如果不是,那么 php 解决方案是什么?