2013-01-21 42 views
3

我一直在努力使这项工作,搜索谷歌和自上周五以来。 我的最终目标是能够拍摄多张带有标题和说明的照片,并将它们上传到服务器,然后显示在网页上。android phonegap相机和图像上传

我到目前为止的内容是:能够给一个图像标题和描述,浏览图库,找到图像并选择它。但是当我这样做的时候,图像随着表单立即上传。我希望能够使用提交按钮来做到这一点。

我也有一个按钮来拍摄图像,并在页面上预览图像。但是当我用相机拍照时,我不知道如何上传表格。我能够使用div和innerHTML调用在屏幕上打印图像数据......但老实说我很迷茫,甚至不知道从哪里开始发布特定的代码片段。因为目前现在存在,我将发布整个页面....

<html> 
<head> 
    <title>File Transfer Example</title> 

    <script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script> 
    <script type="text/javascript" charset="utf-8"> 

     var pictureSource; // picture source 
     var destinationType; // sets the format of returned value 

     // Wait for Cordova to load 
     // 
     document.addEventListener("deviceready", onDeviceReady, false); 

     // Cordova is ready 
     // 
function onDeviceReady() { 
    pictureSource=navigator.camera.PictureSourceType; 
    destinationType=navigator.camera.DestinationType; 
} 


     function browse(){ 
      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 = document.getElementById('file_name').value + ""; 
      params.value3 = document.getElementById('file_description').value + ""; 
      options.params = params; 

      var ft = new FileTransfer(); 
      ft.upload(imageURI, encodeURI("http://site.com/pages/upload.php"), win, fail, options); 
     } 

     function win(r) { 
      console.log("Code = " + r.responseCode); 
      console.log("Response = " + r.response); 
      console.log("Sent = " + r.bytesSent); 
     } 

     function onFileSystemSuccess(fileSystem) { 
      console.log(fileSystem.name); 
     } 

     function onResolveSuccess(fileEntry) { 
      console.log(fileEntry.name); 
     } 

     function fail(evt) { 
      console.log(evt.target.error.code); 
     } 


     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); 
     } 






function capturePhoto() { 
    // Take picture using device camera, allow edit, and retrieve image as base64-encoded string 
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true, 
    destinationType: destinationType.DATA_URL }); 
} 

function onPhotoDataSuccess(imageData) { 
    // console.log(imageData); 

    var smallImage = document.getElementById('smallImage'); 
    smallImage.style.display = 'block'; 
    smallImage.src = "data:image/jpeg;base64," + imageData; 

    var smallTEXT = document.getElementById('smallTEXT'); 
    smallTEXT.style.display = 'block'; 
    smallTEXT.innerHTML = "data:image/jpeg;base64," + imageData; 
} 

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'; 

    largeImage.src = imageURI; 
} 
function capturePhoto() { 
    // Take picture using device camera and retrieve image as base64-encoded string 
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, 
    destinationType: destinationType.DATA_URL }); 
} 



// Called if something bad happens. 
// 
function onFail(message) { 
    alert('Failed because: ' + message); 
} 







     </script> 
</head> 
<body> 


<h1>Example</h1> 
    <p>Upload File</p> 

    <form name ="filename" id="file_name_form" action="#"> 
    Title <br><input type="text" name="name" id="file_name" /><br> 
    Description <br><textarea type="text" name="description" id="file_description" /></textarea> 
    </form> 
    <button onclick="capturePhoto();">Use Camera</button> <br> 
    <button onclick="browse();">browse gallery</button><br> 
    <img style="display:none;width:160px;" id="smallImage" src="" /> 
    <hr> 
    <div id="smallTEXT">ggg</div> 
    <button onclick"uploadPhoto();">submit</button> 


</body> 

+0

您的浏览方法会调用uploadPhoto,因此它的工作方式应该是这样。对Camera.getPicture的调用不能选择多张照片,如果这是你以后的事情。 –

+0

这是问题的一部分,我想上传照片是独立于浏览..我希望相机能够被用来代替画廊..? –

+0

有人请向我解释如何提交这个没有图像?只有表格?请 –

回答

9

根据这样的回答:Phonegap android unable to upload image using fileTransfer 不能直接使用URI .. ..

但是,它似乎uri可以直接使用...(见我的代码如下)

编辑25-7-2013 我得到这个工作有: 调用是这样的:

navigator.camera.getPicture(onPhotoUriSuccess, onFailCamera, { quality: 50, 
     destinationType: pictDestinationType.FILE_URI }); 

和更迭:

function onPhotoUriSuccess(imageUriToUpload){ 
    var url=encodeURI("http://your_url_for_the_post/"); 

    var username='your_user'; 
    var password='your_pwd'; 

    var params = new Object(); 
    params.your_param_name = "something"; //you can send additional info with the file 

    var options = new FileUploadOptions(); 
    options.fileKey = "the_name_of_the_image_field"; //depends on the api 
    options.fileName = imageUriToUpload.substr(imageUriToUpload.lastIndexOf('/')+1); 
    options.mimeType = "image/jpeg"; 
    options.params = params; 
    options.chunkedMode = true; //this is important to send both data and files 

    var headers={'Authorization':"Basic " + Base64.encode(username + ":" + password)}; 
    options.headers = headers; 

    var ft = new FileTransfer(); 
    ft.upload(imageUriToUpload, url, succesFileTransfer, errorFileTransfer, options); 

} 

顺便说一句,我使用的是Apache网络服务器在api站点上,我在这里看到,nginx可能在分块模式下出现问题: PhoneGap chunckedMode true upload error

+0

我也面临类似的问题,但无法解决它。你可以请检查这个问题http://stackoverflow.com/questions/40514847/ – Ironic

0
<!DOCTYPE html> 
<html> 
<head> 
<title>Capture Audio,Image,Video</title> 
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script> 
<script type="text/javascript" charset="utf-8" src="json2.js"></script> 
<script type="text/javascript" charset="utf-8"> 
// Called when capture operation is finished 
// 
function captureSuccess(mediaFiles) { 
var i, len; 
for (i = 0, len = mediaFiles.length; i < len; i += 1) { 
uploadFile(mediaFiles[i]); 
} 
} 
function captureSuccess2(mediaFiles) { 
var i, len; 
for (i = 0, len = mediaFiles.length; i < len; i += 1) { 
uploadFile2(mediaFiles[i]); 
} 
} 
function captureSuccess3(mediaFiles) { 
var i, len; 
for (i = 0, len = mediaFiles.length; i < len; i += 1) { 
uploadFile3(mediaFiles[i]); 
} 
} 
// Called if something bad happens. 
// 
function captureError(error) { 
var msg = "An error occurred during capture: " + error.code; 
navigator.notification.alert(msg, null, "Uh oh!"); 
} 
function captureError2(error) { 
var msg = "An error occurred during capture: " + error.code; 
navigator.notification.alert(msg, null, "Uh oh!"); 
} 
function captureError3(error) { 
var msg = "An error occurred during capture: " + error.code; 
navigator.notification.alert(msg, null, "Uh oh!"); 
} 
// A button will call this function 
// 
function captureAudio() { 
// Launch device audio recording application, 
// allowing user to capture up to 2 audio clips 
navigator.device.capture.captureAudio(captureSuccess, captureError, {limit: 2}); 
} 
function captureImage() 
{ 
// Launch device camera application, 
// allowing user to capture up to 2 images 
navigator.device.capture.captureImage(captureSuccess2, captureError2, {limit: 2}); 
} 
function captureVideo() { 
// Launch device video recording application, 
// allowing user to capture up to 2 video clips 
navigator.device.capture.captureVideo(captureSuccess3, captureError3, {limit: 2}); 
} 
// Upload files to server 
function uploadFile(mediaFile) { 
var win = function (r) { 
alert("Code = " + r.responseCode); 
alert("Bytes Sent = " + r.bytesSent); 
alert("Audio Uploaded"); 
} 
var fail = function (error) { 
alert("An error has occurred: Code = " + error.code); 
alert("upload error source " + error.source); 
alert("upload error target " + error.target); 
} 
var options = new FileUploadOptions(); 
//options.fileKey = mediafile.file; 
options.fileName = mediaFile.file; 
options.mimeType = "audio/wav"; 
fileURL=mediaFile.fullPath; 
var ft = new FileTransfer(); 
ft.upload(fileURL, encodeURI("http://192.168.1.101:80/myfile.php"), win, fail, 
options); 
} 
function uploadFile2(mediaFile) { 
var win = function (r) { 
alert("Code = " + r.responseCode); 
alert("Bytes Sent = " + r.bytesSent); 
alert("Image Uploaded"); 
} 
var fail = function (error) { 
alert("An error has occurred: Code = " + error.code); 
alert("upload error source " + error.source); 
alert("upload error target " + error.target); 
} 
var options = new FileUploadOptions(); 
//options.fileKey = mediafile.file; 
options.fileName = mediaFile.file; 
options.mimeType = "text/plain"; 
ImageURL=mediaFile.fullPath; 
var ft = new FileTransfer(); 
ft.upload(ImageURL, encodeURI("http://192.168.1.101:80/myfile.php"), win, fail, 
options); 
} 
function uploadFile3(mediaFile) { 
var win = function (r) { 
alert("Code = " + r.responseCode); 
alert("Bytes Sent = " + r.bytesSent); 
alert("Video Uploaded"); 
} 
var fail = function (error) { 
alert("An error has occurred: Code = " + error.code); 
alert("upload error source " + error.source); 
alert("upload error target " + error.target); 
} 
var options = new FileUploadOptions(); 
//options.fileKey = mediafile.file; 
options.fileName = mediaFile.file; 
options.mimeType = "video/mpeg"; 
VideoURL=mediaFile.fullPath; 
var ft = new FileTransfer(); 
ft.upload(VideoURL, encodeURI("http://192.168.1.101:80/myfile.php"), win, fail, 
options); 
} 
</script> 
</head> 
<body> 
<center><h1>MCA3B Capture Session</h1></center><br><br> 
<center> <button onclick="captureAudio();">Capture Audio</button> <br><br> 
<button onclick="captureImage();">Capture Image</button> <br><br> 
<button onclick="captureVideo();">Capture Video</button> <br> 
</center> 
</body> 
</html> 

以上是捕获图像,音频和视频并将其上传到本地服务器的代码。