我正在制作带有phonegap的照片上传系统。我有以下脚本,它正确上传。现在我想要实现的是将照片的文件名保存到隐藏字段,以便我可以将其插入到我的数据库中。我有一个工作的PHP插入脚本,但现在我的问题是如何将文件字段的值捕获到隐藏字段或文本字段,以便我可以上传
</script>
function selectPhoto() {
// 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) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
function fail(error) {
alert("An error has occurred: Code = " = error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
</script>
答案 0 :(得分:1)
您可以使用localStorage在HTML5中存储数据。使用localStorage,您可以在任何HTML5页面上检索和传递数据。
链接到localStorage文档:Cordova localStorage
首先从javascript变量options.fileName
检索文件名,并将其存储在localStorage中,然后使用AJAX发送它。
离。 window.localStorage.setItem("key", "options.fileName");
并获取var fileName = window.localStorage.getItem("key");
或者您可以在Multipart表单提交中发送它,如上例所示,并在PHP中检索
$image = "$_FILES["file"]["name"].'.jpg';