是否可以在PhoneGap上收听FileTransfer上传的上传?

时间:2012-03-15 16:30:56

标签: javascript jquery cordova upload

我目前正在使用PhoneGap,我通过我的应用程序继续上传FileTransfer。不幸的是,在发送文件时,我没有通知也没有进度条。

我想知道我们是否可以监听FileTransfer来执行计算进度。 (或者,如果有另一种方法可以阻止用户进行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" />

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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