gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
flutter. how to download file at via flutter web platform? bring only the code related…
- User
flutter. how to download file at via flutter web platform? bring only the code related to sending/receiving requests/responses.
- Assistant
To download a file via Flutter web platform, you can use the
httppackage to send a GET request and receive the response. Here's an example code that downloads a file:import 'package:http/http.dart' as http; import 'dart:io'; Future<void> downloadFile(String url) async { final filename = url.split("/").last; final request = await http.get(Uri.parse(url)); final bytes = request.bodyBytes; if (!Directory("downloads").existsSync()) { Directory("downloads").createSync(); } final file = File("downloads/$filename"); await file.writeAsBytes(bytes); }To use this function, just call
downloadFile()with the URL of the file you want to download:downloadFile("https://example.com/file.pdf");This function first gets the filename from the URL, sends a GET request to the URL using
http.get(), and receives the response as a byte array. Then, it creates a directory named "downloads" if it doesn't exist and saves the file to that directory usingFile.writeAsBytes().