【发布时间】:2014-05-28 21:17:50
【问题描述】:
我创建了一个页面来拍摄图像或从手机图库中选择图像并且工作正常,但我想将这张选择的照片上传到我在 Godaddy 上的服务器。 我使用 Cordova 文件传输上传,通过命令行安装文件传输:
cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git
我输入了一个小代码来上传这张照片,但没有消息提醒(没有错误也没有成功)。
选择图片的代码:
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
var largeImage = document.getElementById('largeImage');
// Unhide image elements
//
largeImage.style.display = 'block';
// Show the captured photo
// The in-line CSS rules are used to resize the image
//
largeImage.src = imageURI;
upload();
}
代码上传功能:
function upload() {
alert('large');
var uploadingImage = document.getElementById('largeImage');
var imgUrl = uploadingImage.src;
window.resolveLocalFileSystemURI(imgUrl, resolveOnSuccess, fsFail);
options = new FileUploadOptions();
// parameter name of file:
options.fileKey = "my_image";
// name of the file:
options.fileName = imgUrl.substr(imgUrl.lastIndexOf('/') + 1);
// mime type:
options.mimeType = "image/jpeg";
params = {val1: "some value", val2: "some other value"};
options.params = params;
ft = new FileTransfer();
ft.upload(fileuri, "http://siencelb.org/raycoding/insurance/avatar", success, fail, options);
}
function resolveOnSuccess(entry) {
fileuri = entry.toURL();
//use fileuri to upload image on server
}
function fsFail(message) {
alert("Error Message: " + message + "Error Code:" + message.target.error.code);
}
我首先有两个按钮来选择图像并将其放入 div largeImage 中,这样就可以了。 选择上传此图片的第二个按钮 注意:会显示 alert('large')。
【问题讨论】:
标签: javascript android cordova