我正在尝试运行this 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);
答案 0 :(得分:0)
options下的fileKey是服务器要查找的名称。在HTML中,您将名称设置为&#34; myfile&#34;并且在javascript中你有&#34;文件&#34;。
<input type="file" name="myfile">
options.fileKey="file";
options.fileKey和属性名是相同的,所以请确保你的PHP在$ _FILES数组中查找&#34;文件&#34;