Flutter中预览doc、pdf等文档

使用file_preview可以像在使用Flutter weidget一样在andorid、ios页面中预览doc、docx、ppt、pptx、xls、xlsx、pdf等文件。

仓库地址

android.gifios.gif

说明

  • andorid使用腾讯TBS服务,支持doc、docx、ppt、pptx、xls、xlsx、pdf、txt、epub文件的预览
  • ios使用WKWebView,WKWebView所支持的均可预览

集成步骤

1、pubspec.yaml

file_preview: ^0.0.2

2、引入

import 'package:file_preview/file_preview.dart';

3、使用

由于使用android使用TBS服务,所以必须在FilePreviewWidget使用前完成初始化,不然无法加载。
如本地无TBS不存在会在初始化时进行下载,会耗时

await FilePreview.initTBS();

使用

 FilePreviewWidget(
          width: 400,//宽
          height: 700,//高
          path: "",//本地路径或者http链接
        )
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
要在Flutter上传本地文件,您需要使用Flutter提供的dart:io库。首先,使用FilePicker库选择要上传的文件。然后,使用http包的MultipartRequest,将文件作为多部分请求附加到请求体。最后,使用http包的HttpClient将请求发送到服务器。以下是一个示例代码: ```dart import 'dart:io'; import 'package:http/http.dart' as http; import 'package:flutter/material.dart'; import 'package:file_picker/file_picker.dart'; class FileUpload extends StatefulWidget { @override _FileUploadState createState() => _FileUploadState(); } class _FileUploadState extends State<FileUpload> { File file; void _openFileExplorer() async { file = await FilePicker.getFile(type: FileType.any); setState(() {}); } Future _uploadFile() async { var request = http.MultipartRequest( 'POST', Uri.parse('your-upload-api-url-here'), ); request.files.add(await http.MultipartFile.fromPath('file', file.path)); request.headers.addAll({'Authorization': 'Bearer your_access_token_here'}); var response = await request.send(); if (response.statusCode == 200) { // handle success } else { // handle error } } @override Widget build(BuildContext context) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ FlatButton( onPressed: _openFileExplorer, child: Text('Select File'), ), file != null ? Text(file.path) : Container(), RaisedButton( onPressed: _uploadFile, child: Text('Upload File'), ), ], ); } } ``` 您需要将“your-upload-api-url-here”替换为您的上传API的实际URL,“Bearer your_access_token_here”替换为您的实际访问令牌。此外,您需要在pubspec.yaml文件添加依赖项file_picker和http。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值