是否有可能在PhoneGap上收听FileTransfer Upload的上传?

问题描述:

我目前正在使用PhoneGap,然后继续通过我的应用程序进行FileTransfer上传。不幸的是,当发送文件时,我没有通知,也没有进度条。是否有可能在PhoneGap上收听FileTransfer Upload的上传?

我想知道我们是否可以监听FileTransfer来执行计算进度。 (或者,如果有另一种方式来防止文件传输进度的用户

谢谢

Yeppao

代码:

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

// Wait for PhoneGap to connect with the device 
// 
document.addEventListener("deviceready",onDeviceReady,false); 

// PhoneGap is ready to be used! 
// 
function onDeviceReady() { 
    pictureSource=navigator.camera.PictureSourceType; 
    destinationType=navigator.camera.DestinationType; 
} 

// Called when a photo is successfully retrieved 
// 
function onPhotoURISuccess(fileURI) { 
    file = fileURI; 

} 


function uploadFile() { 
    alert('up begin'); 
    function uploadSuccess(success) 
    { 
    alert(success); 
    } 

    function uploadError(error) 
    { 
    alert(error); 
    } 

    var options = new FileUploadOptions(); 
    options.fileKey="file"; 
    options.fileName=file.substr(file.lastIndexOf('/')+1); 
    options.mimeType="application/octet-stream"; 

    var params = new Object(); 
    params.title = $("#video-name").val(); 
    params.value2 = "param"; 

    options.params = params; 
    options.chunkedMode = true; 

    var ft = new FileTransfer(); 

    ft.upload(file, "http://myserver/file.php", uploadSuccess, uploadError, options); 

} 

// A button will call this function 
// 
function getFile() { 
    // Retrieve image file location from specified source 
    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
     destinationType: destinationType.FILE_URI, 
     sourceType: pictureSource.PHOTOLIBRARY, 
     mediaType: navigator.camera.MediaType.ALLMEDIA }); 
} 

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

这里的HTML部分:

<div id="file-name"></div> 
    <button style="height: 100px;background-color: #FFF;" data-role="button" onclick="getFile();">From Photo Library</button><br> 
    <input type="text" value="" id="video-name" name="video-name" /> 
<input style="background-color: #FFF;" type="button" id="test" onclick="uploadFile()" name="test" value="Ok" /> 
+0

发布您的代码.... – Brad 2012-03-15 16:39:54

+0

确定它已完成@Brad。 – Yeppao 2012-03-15 17:07:55

有一个进度条可能会很棘手,但如果你使用jQuery手机,你可以使用$ .mobile.showPageLoadingMsg和$ .mobile.hidePageLoadingMsg

我还没有试过这个,但是,也许你可以从服务器端做到这一点。通过不断请求一个页面来向您报告正在上传的文件的当前更新状态。