我可以将我的PhoneGap应用程序中的图像上传到我的网络服务器。但是我想要做的就是拍照而不是自动上传,但是等到我在表格输入字段中输入我的名字,然后当我点击提交时,它会提交表格并上传图像。处理该文件的php脚本然后将名称连接到图像名称,以便我可以告诉谁上传了它。
我希望人们能够为比赛提交照片,但我需要一种方法将用户与他们提交的照片进行匹配。如果它不太复杂,也许另一种方法可以做到这一点?
这是我上传的javascript
function captureSuccess(mediaFiles) {
var i, len;
for (i = 0, len = mediaFiles.length; i < len; i += 1) {
uploadFile(mediaFiles[i]);
}
}
// Called if something bad happens.
//
function captureError(error) {
var msg = 'Something went wrong'
navigator.notification.alert(msg, null, 'Uh oh!');
}
// A button will call this function
//
function captureImage() {
// Launch device camera application,
// allowing user to capture up to 2 images
navigator.device.capture.captureImage(captureSuccess, captureError, {limit: 2});
}
// Upload files to server
function uploadFile(mediaFile) {
var ft = new FileTransfer(),
path = mediaFile.fullPath,
name = mediaFile.name;
ft.upload(path,
"http://www.example.com/upload.php",
function(result) {
console.log('Upload success: ' + result.responseCode);
console.log(result.bytesSent + ' bytes sent');
var user_id = Math.floor(Math.random()*1001)
var day_uploaded = new Date().getDay();
var hour_uploaded = new Date().getHours();
var minute_uploaded = new Date().getMinutes();
var sec_uploaded = new Date().getSeconds();
var msg = 'Your file was uploaded! Your picture ID is ' + day_uploaded + hour_uploaded + minute_uploaded + sec_uploaded;
navigator.notification.alert(msg, null, 'Thanks!');
},
function(error) {
console.log('Error uploading file ' + path + ': ' + error.code);
},
{ fileName: name });
}
答案 0 :(得分:1)
是的,你可以
var _uploadPhoto = function(id, name, data) {
var _data = {
'id' : id,
'fileName' : name,
'image' : data
};
$.ajax({
type : 'POST',
data : JSON.stringify(_data),
url : 'your_url'
success : function(data) { },
error: function(error) { }
});
};