【发布时间】:2022-12-16 01:51:07
【问题描述】:
我正在尝试使用 dio.download 下载 *.xlsx 文件,但它抛出了错误: 未处理的异常:FileSystemException:无法打开文件,路径 = '/storage/emulated/0/Android/data/com.example.foodagator_app/files/file.xlsx'(操作系统错误:没有这样的文件或目录,errno = 2)
try/catch 块中的另一个错误: FileSystemException:创建失败,路径 = '文件:''(操作系统错误:只读文件系统,errno = 30)
我在androidmanifest中写了外部存储的权限,也尝试了临时目录,但它不起作用。谁能帮我这个?这是我的代码
void download() async {
var tempDir = await getExternalStorageDirectory();
File file = File(tempDir!.path + '/file.xlsx');
try {
Response response = await dio.download(
url,
file,
options: Options(
responseType: ResponseType.bytes,
followRedirects: false,
),
);
var raf = file.openSync(mode: FileMode.write);
// response.data is List<int> type
raf.writeFromSync(response.data);
await raf.close();
} catch (e) {
print('Error is: $e');
}
}
void readFile() async {
var tempDir = await getExternalStorageDirectory();
var filePath = tempDir!.path + "/file.xlsx";
var bytes = File(filePath).readAsBytesSync();
var decoder = SpreadsheetDecoder.decodeBytes(bytes, update: true);
for (var table in decoder.tables.keys) {
print(table);
print(decoder.tables[table]!.maxCols);
print(decoder.tables[table]!.maxRows);
for (var row in decoder.tables[table]!.rows) {
print('$row');
}
}
}
【问题讨论】: