Flutter 颤振下载部分文件从谷歌驱动器和一个驱动器
颤振无法从Google Drive或OneDrive、zip或其他方式下载文件。 这些文件只是部分下载,而来自网站等的大多数其他链接的文件是完全下载的 例如,这个GoogleDrive链接只能获得10%的文件下载,而小说的另一个URL将获得100% 使用的软件包:Flutter 颤振下载部分文件从谷歌驱动器和一个驱动器,flutter,dart,download,dio,flutter-http,Flutter,Dart,Download,Dio,Flutter Http,颤振无法从Google Drive或OneDrive、zip或其他方式下载文件。 这些文件只是部分下载,而来自网站等的大多数其他链接的文件是完全下载的 例如,这个GoogleDrive链接只能获得10%的文件下载,而小说的另一个URL将获得100% 使用的软件包: http:^0.12.2 dio:^3.0.10 路径\提供程序:^1.6.24 谷歌驱动器网址:https://drive.google.com/file/d/1xnhT8mzMeU-wRemt1sNFR0DJt2MNmSC8/
- http:^0.12.2
- dio:^3.0.10
- 路径\提供程序:^1.6.24
- 预期大小:myColorsDIO.zip:594545,myColorsHTTP.zip:594545, 1342DIO.zip:778512、1342HTTP.zip:778512
- 获得的尺寸: myColorsDIO.zip:63411,mycolorshtp.zip:706771342dio.zip:778512, 1342HTTP.zip:778512
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import 'dart:io';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(title: 'Flutter Demo', theme: ThemeData(primarySwatch: Colors.blue,visualDensity: VisualDensity.adaptivePlatformDensity,), home: MyHomePage(),);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Downloading Files'),),
body: Center(child: Text('Press the floating button to download files',),),
floatingActionButton: FloatingActionButton(
onPressed: () { _downloadFile();},
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
Future<File> _downloadFile() async {
String myUrl = 'https://drive.google.com/file/d/1xnhT8mzMeU-wRemt1sNFR0DJt2MNmSC8/view?usp=sharing';
String my7ZIPUrl = 'https://www.gutenberg.org/files/1342/1342-h.zip';
String myDownloadDirectory = ((await getApplicationDocumentsDirectory()).path);
String myDownloadedGoogleFile1 = 'myColorsDIO.zip';
String myDownloadedGoogleFile2 = 'myColorsHTTP.zip';
String myDownloadedfromgutenberg1 = '1342DIO.zip';
String myDownloadedfromgutenberg2 = '1342HTTP.zip';
print('Download Directory: $myDownloadDirectory');
//Downloading Google Drive zip file via DIO
Dio dio = Dio();
await dio.download(
myUrl,
'$myDownloadDirectory/$myDownloadedGoogleFile1',
onReceiveProgress: (rcv, total) {print('received: ${rcv.toStringAsFixed(0)} out of total: ${total.toStringAsFixed(0)}');}
);
await dio.download(
my7ZIPUrl,
'$myDownloadDirectory/$myDownloadedfromgutenberg1',
onReceiveProgress: (rcv, total) {print('received: ${rcv.toStringAsFixed(0)} out of total: ${total.toStringAsFixed(0)}');}
);
//Downloading Google Drive zip file via HTTP
var req = await http.Client().get(Uri.parse(myUrl));
var file = File('$myDownloadDirectory/$myDownloadedGoogleFile2',);
file.writeAsBytes(req.bodyBytes);
//Downloading Google Drive zip file via HTTP
var req1 = await http.Client().get(Uri.parse(my7ZIPUrl));
var file1 = File('$myDownloadDirectory/$myDownloadedfromgutenberg2',);
file1.writeAsBytes(req1.bodyBytes);
print('Expected size: myColorsDIO.zip: 594,545 , myColorsHTTP.zip: 594,545 , 1342DIO.zip: 778512, 1342HTTP.zip: 778512');
print('myColorsDIO.zip: ${await File('$myDownloadDirectory/$myDownloadedGoogleFile1',).length()}, myColorsHTTP.zip: ${await File('$myDownloadDirectory/$myDownloadedGoogleFile2',).length()}, 1342DIO.zip: ${await File('$myDownloadDirectory/$myDownloadedfromgutenberg1',).length()}, 1342HTTP.zip: ${await File('$myDownloadDirectory/$myDownloadedfromgutenberg2',).length()}');
return null;
}
}
import'包:dio/dio.dart';
进口“包装:颤振/材料.省道”;
将“package:http/http.dart”导入为http;
导入“package:path_provider/path_provider.dart”;
导入“dart:io”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回MaterialApp(标题:“颤振演示”,主题:主题数据(原始样本:颜色。蓝色,视觉密度:视觉密度。自适应平台密度,),主页:MyHomePage(),);
}
}
类MyHomePage扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(“下载文件”),),
正文:居中(子项:文本('按浮动按钮下载文件',),),
浮动操作按钮:浮动操作按钮(
onPressed:(){u downloadFile();},
工具提示:“增量”,
子:图标(Icons.add),
),//此尾随逗号使生成方法的自动格式设置更方便。
);
}
Future\u downloadFile()异步{
字符串myUrl=https://drive.google.com/file/d/1xnhT8mzMeU-wRemt1sNFR0DJt2MNmSC8/view?usp=sharing';
字符串my7ZIPUrl=https://www.gutenberg.org/files/1342/1342-h.zip';
字符串myDownloadDirectory=((等待getApplicationDocumentsDirectory()).path);
字符串myDownloadedGoogleFile1='myColorsDIO.zip';
字符串myDownloadedGoogleFile2='myColorsHTTP.zip';
字符串myDownloadedfromgutenberg1='1342DIO.zip';
字符串myDownloadedfromgutenberg2='1342HTTP.zip';
打印('downloaddirectory:$myDownloadDirectory');
//通过DIO下载Google Drive zip文件
Dio Dio=Dio();
等待下载(
我的网址,
“$myDownloadDirectory/$myDownloadedGoogleFile1”,
onReceiveProgress:(rcv,total){print('received:${rcv.toStringAsFixed(0)}总计:${total.toStringAsFixed(0)}');}
);
等待下载(
my7ZIPUrl,
“$myDownloadDirectory/$myDownloadedfromgutenberg1”,
onReceiveProgress:(rcv,total){print('received:${rcv.toStringAsFixed(0)}总计:${total.toStringAsFixed(0)}');}
);
//通过HTTP下载Google Drive zip文件
var req=wait http.Client().get(Uri.parse(myUrl));
var file=file(“$myDownloadDirectory/$myDownloadedGoogleFile2”,);
file.writeAsBytes(req.bodyBytes);
//通过HTTP下载Google Drive zip文件
var req1=wait http.Client().get(Uri.parse(my7ZIPUrl));
var file1=File(“$myDownloadDirectory/$myDownloadedfromgutenberg2”,);
file1.writeAsBytes(req1.bodyBytes);
打印(“预期大小:myColorsDIO.zip:594545,myColorsHTTP.zip:5945451342dio.zip:7785121342http.zip:778512”);
print('myclorsdio.zip:${await File('$myDownloadDirectory/$mydownloadegooglefile1',).length()},myclorshttp.zip:${await File('$myDownloadDirectory/$mydownloadegoglefile2',).length()},1342DIO.zip:${await File('$myDownloadDirectory/$myDownloadedfromgutenberg1',).length()},1342HTTP.zip:${await File(“$myDownloadDirectory/$myDownloadedfromgutenberg2',).length()}”);
返回null;
}
}
共享链接无法直接工作,请将其放在浏览器中,当提示您下载时,这就是有效的URL。您尝试下载的链接将下载zip文件的预览页,而不是文件本身。@Christopher如何获得文件的直接链接?我尝试了共享URL做了一些尝试。@ChristopherE自我调查并提出一个问题,因此如果您在实现方面有问题。@Christophermore那么这是我的责任,我道歉