2017-08-27 55 views

回答

0

您可以輕鬆地使用File TransferFile本地插件爲您的任務。

從官方文檔中提取:

離子科爾多瓦插件添加科爾多瓦 - 插件 - 文件傳輸

NPM安裝--save @離子本地/文件傳輸

離子cordova插件添加cordova插件文件

npm install --save @ ionic-native/file

.TS

import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer'; 
import { File } from '@ionic-native/file'; 

constructor(private transfer: FileTransfer, private file: File) { } 


const fileTransfer: FileTransferObject = this.transfer.create(); 

// Upload a file: 
fileTransfer.upload(..).then(..).catch(..); 

// Download a file: 
fileTransfer.download(..).then(..).catch(..); 

// Abort active transfer: 
fileTransfer.abort(); 

// full example 
upload() { 
    let options: FileUploadOptions = { 
    fileKey: 'file', 
    fileName: 'name.jpg', 
    headers: {} 
    } 

    fileTransfer.upload('<file path>', '<api endpoint>', options) 
    .then((data) => { 
    // success 
    }, (err) => { 
    // error 
    }) 
} 

download() { 
    const url = 'http://www.example.com/file.pdf'; 
    fileTransfer.download(url, this.file.dataDirectory + 'file.pdf').then((entry) => { 
    console.log('download complete: ' + entry.toURL()); 
    }, (error) => { 
    // handle error 
    }); 
} 
+0

那就是文件本身轉移,但如何獲取該文件。 fileChooser僅在Android中受支持。 phoneGap FilePicker返回一個我似乎無法用來讀入文件內容的URI。 – noor

+0

您可以使用'download()'方法,如上面我的帖子所示。 – Sampath