2015-04-14 114 views

回答

1

希望,你還在尋找一個答案。

在我的一個應用程序中,我使用兩個函數將輸入#文件保存爲pdf,但您也可以將這些函數重寫爲其他MIME類型。直到今天,這些功能都可以在Android和iOS平臺上運行。

功能getAppURI用於獲取實際的應用程序文件夾名稱,您可以在其中複製文件,它請求應用程序的高速緩存文件夾並替換最後一個子文件夾名稱以獲得您的基本uri應用程序,其非常簡單。

// get your app-root-folder-name, for instance Android: file:///storage/emulated/0/Android/data/YOUR_APP_NAMESPACE/ 
function getAppURI(isAndroid,callback) { 
    if(isAndroid) { 
     window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (filesystem) { 
       var cacheDir = filesystem.root.toURL(); 
       var startPointCacheFolderName = cacheDir.match(/\/\w+\/$/) [0]; 
       callback(cacheDir.replace(startPointCacheFolderName, '') + '/'); 
      }, function (error) { 
       console.log('no access to app-filesystem', error); 
      } 
     ); 
    } 
    else{ 
     // iOS 
     // just request the filesystem so that you really have access to it 
     window.resolveLocalFileSystemURL(cordova.file.documentsDirectory, 
      function(entry){ 
       callback(entry.nativeURL); 
      }, 
      function(error){ 
      console.log("no access to filesystem",error); 
     }); 
    } 
} 

實際的複製操作是用savePDFFromInputFile函數完成的。此函數接受四個參數,您可以基本控制目標以及如何命名該複製的PDF文件。它檢查它是否爲pdf,獲取它的原始文件名(以後可以使用)並創建一個Blob,其中包含FileReader的二進制數組結果。

但是,在可以複製輸入#文件之前,會創建一個新的空文件。之後,剛創建的Blob被寫入這個空文件。完!

function savePDFFromInputFile(inputHTMLElement, appURI, sourcename, callback) { 
    // check whether its a pdf 
    if (inputHTMLElement.files[0] && 
     inputHTMLElement.files[0].type && 
     inputHTMLElement.files[0].type.indexOf('pdf') !== - 1) { 
     var filename = ""; 
     var reader = new FileReader(); 
     var fullPath = inputHTMLElement.value; 
     if (fullPath) { 
      // get original filename that can be used in the callback 
      var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/')); 
      var filename = fullPath.substring(startIndex); 
      if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) { 
       filename = filename.substring(1); 
      } 
     } 
     reader.onload = function() { 
      // the pdf-file is read as array-buffer 
      // this array-buffer can be put into a blob 
      var blob = new Blob([reader.result], { 
       type: 'application/pdf' 
      }); 
      // create empty file 
      $cordovaFile.createFile(appURI, sourcename, true).then(function (success) { 
       // write to this empty file 
       $cordovaFile.writeExistingFile(appURI, sourcename, blob, true).then(function (success) { 
        callback({ 
         name: filename, 
         source: sourcename 
        }); 
       }, function (error) { 
        console.log(error); 
       }); 
      }, function (error) { 
       console.log(error); 
      }); 
     }; 
     reader.readAsArrayBuffer(inputHTMLElement.files[0]); 
    } 
} 

這是兩種功能如何使用的例子:

// test for android-plattform 
var isAndroid = true; 
getAppURI(isAndroid, function(appURI){ 
    var inputFileElement = $('ID OR CLASS OF INPUT#FILE')[0]; // or use document.getElementById(...) 
    var sourcename = (new Date()).getTime() + '.pdf'; 
    savePDFFromInputFile(inputFileElement, appURI, sourcename, function(copiedPDF){ 
     console.log("pdf copied successfully",copiedPDF.name,copiedPDF.source); 
    }); 
}); 

希望它能幫助!