首页 > 解决方案 > 使用 cordova 文件传输附加 pdf/word 文件

问题描述

我正在使用 Cordova 文件传输插件来上传和下载 jpf/png 文件。下面是我这样做的代码。

$scope.getFileFromGallery = function () {

var options = {
  maximumImagesCount: 1,
  quality: 50,
  destinationType: Camera.DestinationType.FILE_URI,
  sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
  allowEdit: false,
  encodingType: Camera.EncodingType.JPEG,
  popoverOptions: CameraPopoverOptions,
  saveToPhotoAlbum: false,
  correctOrientation: true
};

$cordovaCamera.getPicture(options).then(function (imageURI) {

  window.resolveLocalFileSystemURL(imageURI, function (fileEntry) {
    fileEntry.file(function (file) {

      $scope.uploadFile(file, imageURI);

    }, function () {
      $scope.showToastMsg("Error Occurred");
    });
  });

}, function (error) {
  // error
});
};
 $scope.uploadFile = function (file, imageURI) {

LoaderFactory.showLoader();
var uri = encodeURI(baseURL + "api/files/upload");

var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = file.name;
options.mimeType = file.type;

var headers = { 'Authorization': getStorage('accessToken') };

options.headers = headers;

var ft = new FileTransfer();
ft.onprogress = function (progressEvent) {
  if (progressEvent.lengthComputable) {
    $scope.percentage = Math.floor(progressEvent.loaded / progressEvent.total * 100);
    $scope.showToastMsg("Uploading Progress  " + $scope.percentage + "%");

    if (myTimeOut)
      clearTimeout(myTimeOut);

    var myTimeOut = setTimeout(function () {
      LoaderFactory.hideLoader();
    }, 5000);

  } else {
  }
};
ft.upload(file.localURL, uri, win, fail, options);
$scope.imageURL = '';
$scope.imageURL = {
  name: file.name,
  URL: imageURI
};



};

但我没有得到任何线索来更改这段代码以选择和附加 pdf 文件或 word 文件。我也试过https://www.tutorialspoint.com/cordova/cordova_file_transfer.htm。关于如何修改上述内容以选择和上传 pdf 和 word 文件的任何线索。我在 Ionic1 中这样做。

标签: javascriptfilecordovaionic-framework

解决方案


推荐阅读