我在android phonegap做项目,我想上传pic到服务器.
但我不知道,我应该把这段代码放在哪里.
我无法显示任何上传照片的按钮,请帮忙.
我是新手.我从phonegap文档中查看了这段代码.
我想这几个小时,但无法得到更好的解决方案.
这是我的第一个android phonegap项目.
码:
<head>
<script type="text/javascript" charset="utf-8" src="cordova-2.4.0.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
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 = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("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>
</head>
<body>
<h1>Example</h1>
<p>Upload File</p>
</body>
Run Code Online (Sandbox Code Playgroud)
A. *_*ães 10
您可以使用下一个代码解决问题:
<script type="text/javascript">
function uploadFromGallery() {
// 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)+'.png';
options.mimeType="text/plain";
var params = new Object();
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("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>
</head>
<body>
<a data-role="button" onClick="uploadFromGallery();">Upload from Gallery</a>
</body>
Run Code Online (Sandbox Code Playgroud)
在这篇文章中查看更多信息:https: //stackoverflow.com/a/13862151/1853864
请尝试以下代码.
// A button will call this function
// To capture photo
function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(uploadPhoto, onFail, {
quality: 50, destinationType: Camera.DestinationType.FILE_URI
});
}
// A button will call this function
// To select image from gallery
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(uploadPhoto, onFail, { quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
}
function uploadPhoto(imageURI) {
//If you wish to display image on your page in app
// Get image handle
var largeImage = document.getElementById('largeImage');
// Unhide image elements
largeImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
largeImage.src = imageURI;
var options = new FileUploadOptions();
options.fileKey = "file";
var userid = '123456';
var imagefilename = userid + Number(new Date()) + ".jpg";
options.fileName = imagefilename;
options.mimeType = "image/jpg";
var params = new Object();
params.imageURI = imageURI;
params.userid = sessionStorage.loginuserid;
options.params = params;
options.chunkedMode = false;
var ft = new FileTransfer();
var url = "Your_Web_Service_URL";
ft.upload(imageURI, url, win, fail, options, true);
}
//Success callback
function win(r) {
alert("Image uploaded successfully!!");
}
//Failure callback
function fail(error) {
alert("There was an error uploading image");
}
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
Run Code Online (Sandbox Code Playgroud)
在HTML页面中创建一个按钮,onclick根据您的要求在其上执行事件调用.
capturePhoto()捕获和上传照片的功能.getPhoto()函数从图库中获取图像.HTML应该包含如下按钮:
<input name="button" type="button" onclick="capturePhoto()" value="Take Photo"/>
<input name="button" type="button" onclick="getPhoto();" value="Browse" />
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.
| 归档时间: |
|
| 查看次数: |
33017 次 |
| 最近记录: |