1
我不知道從哪裏開始,我已經使用了PhoneGap的文件選擇,但不知道如何讀文件和後記發送內容到服務器在IONIC 3中,如何在ios上上傳一個文件,如word或pdf?
我不知道從哪裏開始,我已經使用了PhoneGap的文件選擇,但不知道如何讀文件和後記發送內容到服務器在IONIC 3中,如何在ios上上傳一個文件,如word或pdf?
您可以輕鬆地使用File Transfer和File本地插件爲您的任務。
從官方文檔中提取:
離子科爾多瓦插件添加科爾多瓦 - 插件 - 文件傳輸
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
});
}
那就是文件本身轉移,但如何獲取該文件。 fileChooser僅在Android中受支持。 phoneGap FilePicker返回一個我似乎無法用來讀入文件內容的URI。 – noor
您可以使用'download()'方法,如上面我的帖子所示。 – Sampath