how to use http.StreamedRequest to download file?
https://stackoverflow.com/questions/51195648/how-do-i-use-streamedrequest-in-dart-http-library-to-upload-a-file might help
this is upload-a-file!!! I need download file
You get a StreamedResponse for the download.
Your question might be incorrect. Perhaps you want a StreamedResponse instead of StreamedRequest, but the link shows that as well.
You get a StreamedResponse for the download.
Your question might be incorrect. Perhaps you want a StreamedResponse instead of StreamedRequest, but the link shows that as well.
this link is for upload-a-file! ok!
I am using StreamedResponse, but the result is not returned
I'd suggest you create a new StackOverflow question with more details.
I'd suggest you create a new StackOverflow question with more details.
void downloadByHttp() {
var file = new File("Http_Download.mp3");
if(file.existsSync()) {
file.deleteSync();
}
IOSink ios = file.openWrite(mode: FileMode.APPEND);
Http.get(url)
.then((Http.Response response) {
ios.add(response.bodyBytes);
}).whenComplete(() => ios.close());
}
The above can be downloaded successfully, but can not return to the download data from time to time, I looked at the data and found that StreamedRequest can be implemented, with StreamedRequest how to achieve the download?
StreamedRequest is used for POST requests where you are uploading a stream of bytes. I don't think it will help you here.
If you want to stream a download you're interested in StreamedResponse instead - this is assuming you _don't_ want it all in memory at once and want to write it to the file as it comes in.
void downloadByHttp() async {
var client = http.Client();
var request = http.Request("GET", url);
var response = await client.send(request);
var sink = file.openWrite();
await response.stream.pipe(sink);
sink.close();
}
but can not return to the download data from time to time
You can open the file again and read it if you need the data back in memory. Though if this is the case you already had it in memory once...
Future<List<int>> downloadByHttp() async {
var file = new File("Http_Download.mp3");
if(file.existsSync()) file.deleteSync();
var ios = file.openWrite(mode: FileMode.APPEND);
var response = await http.get(url);
var bytes = response.bodyBytes;
ios.add(bytes);
ios.close();
return bytes; // Caller can do whatever it needs with these bytes without rereading the file
}
As @zoechi mentioned, this type of question is better handled on stack overflow.
Just wondering, how can we set to download bigger chunks? currently i only am getting around 2k bytes per chunk. i wish to increase it like 100k or 500k bytes
Most helpful comment
StreamedRequestis used for POST requests where you are uploading a stream of bytes. I don't think it will help you here.If you want to stream a download you're interested in
StreamedResponseinstead - this is assuming you _don't_ want it all in memory at once and want to write it to the file as it comes in.You can open the file again and read it if you need the data back in memory. Though if this is the case you already had it in memory once...
As @zoechi mentioned, this type of question is better handled on stack overflow.