我需要将一个PDF文件发布到一个远程REST API,我不能为我的生活找到它.无论我做什么,服务器都会响应我尚未将对象与file
参数相关联.假设我有一个叫做PDF的文件test.pdf
.这是我到目前为止所做的事情:
// Using an HttpClientRequest named req
req.headers.contentType = new ContentType('application', 'x-www-form-urlencoded');
StringBuffer sb = new StringBuffer();
String fileData = new File('Test.pdf').readAsStringSync();
sb.write('file=$fileData');
req.write(sb.toString());
return req.close();
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试了几乎我write()
对请求的数据的每个组合和编码,但无济于事.我尝试发送它codeUnits
,我已经尝试使用a编码UTF8.encode
,我尝试使用a编码它Latin1Codec
,一切.我很难过.
任何帮助将不胜感激.
您可以使用MultipartRequest从HTTP包:
var uri = Uri.parse("http://pub.dartlang.org/packages/create");
var request = new http.MultipartRequest("POST", url);
request.fields['user'] = 'john@doe.com';
request.files.add(new http.MultipartFile.fromFile(
'package',
new File('build/package.tar.gz'),
contentType: new ContentType('application', 'x-tar'));
request.send().then((response) {
if (response.statusCode == 200) print("Uploaded!");
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3580 次 |
最近记录: |