本文介绍了如何在Dart http库中使用StreamedRequest上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过非 multipart / form-data
请求上传文件,如下所示:
I want to upload a file via a non-multipart/form-data
request like this:
POST http://127.0.0.1/upload
Cache-Control: no-cache
< /path/to/file/in/disk
(我进行了测试,并成功从JetBrain上传了一个文件Rider的REST客户端连接到我的REST端点。)
(I tested and it successfully uploaded a file from JetBrain Rider's REST client to my REST endpoint.)
有一个类,但是我找不到任何构造函数或setter来将字节流或文件内容插入其中。
There is a StreamedRequest
class in http
package but I didn't find any constructor or setter to plug a byte stream or file content into it.
如何使用 StreamedRequest
要在Dart中上传文件吗?
How do I use StreamedRequest
to upload a file in Dart?
推荐答案
在我的一些旧代码中,我使用
In some older code of mine I use
/// Send a POST request to the Docker service.
Future<http.ByteStream> _streamRequestStream(
String path, Stream<List<int>> stream,
{Map<String, String> query}) async {
assert(stream != null);
final url = serverReference.buildUri(path, query);
final request = new http.StreamedRequest('POST', url)
..headers.addAll(headersTar);
stream.listen(request.sink.add);
final http.BaseResponse response =
await request.send().then(http.Response.fromStream);
if (response.statusCode < 200 || response.statusCode >= 300) {
throw new DockerRemoteApiError(
response.statusCode, response.reasonPhrase, null);
}
return (response as http.StreamedResponse).stream;
}
这可能会做您想要的事
它使用 http
包
import 'package:http/http.dart' as http;
这篇关于如何在Dart http库中使用StreamedRequest上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!