我正在开发一个项目,需要在点击按钮后从URL下载文件,并将其存储到手机存储空间(可能是下载文件夹)。
有什么好的建议吗?被下载的文件类型也不总是相同的,可以是图片或PDF等任何文件。
我正在开发一个项目,需要在点击按钮后从URL下载文件,并将其存储到手机存储空间(可能是下载文件夹)。
有什么好的建议吗?被下载的文件类型也不总是相同的,可以是图片或PDF等任何文件。
final taskId = await FlutterDownloader.enqueue(
url: 'your download link',
savedDir: 'the path of directory where you want to save downloaded files',
showNotification: true, // show download progress in status bar (for Android)
openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);
Future<String> downloadFile(String url, String fileName, String dir) async {
HttpClient httpClient = new HttpClient();
File file;
String filePath = '';
String myUrl = '';
try {
myUrl = url+'/'+fileName;
var request = await httpClient.getUrl(Uri.parse(myUrl));
var response = await request.close();
if(response.statusCode == 200) {
var bytes = await consolidateHttpClientResponseBytes(response);
filePath = '$dir/$fileName';
file = File(filePath);
await file.writeAsBytes(bytes);
}
else
filePath = 'Error code: '+response.statusCode.toString();
}
catch(ex){
filePath = 'Can not fetch url';
}
return filePath;
}
对于 Android,请不要忘记在清单文件中添加以下行,否则在构建 APK 后将无法在实际设备上运行:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
static var httpClient = new HttpClient();
Future<File> _downloadFile(String url, String filename) async {
var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
String dir = (await getApplicationDocumentsDirectory()).path;
File file = new File('$dir/$filename');
await file.writeAsBytes(bytes);
return file;
}
FlutterChina的Dio似乎是不错的选择。https://github.com/flutterchina/dio/。它功能强大,可以让您获取下载/上传进度的选项。
Future<String?> _saveFileToDevice(String filename, List<int> data) async {
// Get the path to the app's documents directory
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
}
var dir = Platform.isAndroid
? '/storage/emulated/0/Download'
: (await getApplicationDocumentsDirectory()).path;
// Create the file and write the data to it
var file = File('$dir/$filename');
bool alreadyDownloaded = await file.exists();
String incrementCount(String fileName) {
int dotIndex = fileName.lastIndexOf('.');
String newFileName = fileName.substring(0, dotIndex) +
"(${count += 1})" +
fileName.substring(dotIndex);
return newFileName;
}
if (alreadyDownloaded) {
String newFileName = incrementCount(file.path);
var newFile = File('$newFileName');
await newFile.writeAsBytes(data, flush: true);
String subStringFileName = newFileName.substring(29);
CommonWidgets.makeToast(
fontSize: 14,
toastMsg: '${subStringFileName} saved to Downloads Folder');
file = newFile;
print('modified updating ....--> $file');
} else {
await file.writeAsBytes(data, flush: true);
CommonWidgets.makeToast(
fontSize: 14,
toastMsg: '${filename} saved to Downloads Folder');
}
return 'file://${file.path}';
}
这个函数将下载Uint8List字节,我们需要将文件转换为Uint8List字节