我想下载文件使用url与Flutter 翼Dio
的帮助.
final imgUrl = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
var dio = Dio();
RaisedButton.icon(
onPressed: (){
download2(dio, imgUrl, "./example/boo2.pdf");
},
icon: Icon(Icons.file_download,color: Colors.white,),
color: Colors.green,
textColor: Colors.white,
label: Text('Dowload Invoice')
)
Future download2(Dio dio, String url, String savePath) async {
try {
Response response = await dio.get(
url,
onReceiveProgress: showDownloadProgress,
//Received data with List<int>
options: Options(
responseType: ResponseType.bytes,
followRedirects: false,
validateStatus: (status) { return status < 500; }
),
);
print(response.headers);
File file = File(savePath);
var raf = file.openSync(mode: FileMode.write);
// response.data is List<int> type
raf.writeFromSync(response.data);
await raf.close();
} catch (e) {
print(e);
}
}
void showDownloadProgress(received, total) {
if (total != -1) {
print((received / total * 100).toStringAsFixed(0) + "%");
}
}
我发现了这个错误.
操作系统错误:权限被拒绝,错误号=13Flutter