在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