2014-09-22 78 views
0

在外部网站有一个输入(类型 - 文件)字段,我需要上传一个图像到这个输入。 在android中,我使用WebChromeClient和openFileChooser(ValueCallback uploadMsg,String acceptType,String capture)函数完成了此任务。 我不知道如何解决这个任务在phonegap /科尔多瓦。Phonegap /科尔多瓦上传图像文件

我的代码是:

function initialize() { 
    document.addEventListener('deviceready', onDeviceReady, false); 
    var ref = null; 
} 
function onDeviceReady() { 

    ref = window.open('http://example.com', 
      '_blank', 'location=no', 'EnableViewPortScale=yes'); 
    ref.addEventListener('loadstop', LoadStop); 

} 

function LoadStop(event) { 
    if (event.url == 'http://example.com/Add') { 
     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) { 
    console.log("imageURI----: " + imageURI); 

    var options = new FileUploadOptions(); 
    options.fileKey = "file"; 
    options.fileName = "IMAG0475"; 
    options.mimeType = "image/*"; 

    var params = new Object(); 
    options.params = params; 

    var ft = new FileTransfer(); 
    ft.upload(imageURI, 
      encodeURI("http://example/Add"), 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); 
} 
+0

检查此问题http://stackoverflow.com/questions/24139295/form-file-upload-phonegap/24142596#24142596 – Aravin 2014-09-22 10:43:26

回答

0

默认情况下科尔多瓦应用程序没有在Android手机,例如,SD卡,外部文件访问所有存储位置。

要解决这个问题,下面的线复制到您的科尔多瓦项目的config.xml中,

<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root"/> 

的Config.xml可以在/platforms/android/res/xml/config.xml找到

只需添加上述行并再次构建项目。

+0

没有解决我的问题 – dirtrider 2014-09-22 11:15:21

相关问题