问题描述
我需要将一个PDF文件发布到一个远程REST API,我不能为我的生活找出来。不管我做什么,服务器响应我还没有将一个对象与文件
参数相关联。让我们说,我有一个PDF test.pdf
。这是我到目前为止做的:
I need to post a PDF file to a remote REST API, and I can't for the life of me figure it out. No matter what I do, the server responds that I have not yet associated an object with the file
parameter. Let's say that I have a PDF called test.pdf
. This is what I've been doing so far:
// 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();
到目前为止,我已经尝试过几乎每个组合和编码的数据, c> write()到请求,但是没有效果。我已经尝试发送它作为 codeUnits
,我已经尝试编码它使用 UTF8.encode
,我尝试使用 Latin1Codec
编码它,一切。
Thus far, I've tried virtually every combination and encoding of the data that I write()
to the request, but to no avail. I've tried sending it as codeUnits
, I've tried encoding it using a UTF8.encode
, I've tried encoding it using a Latin1Codec
, everything. I'm stumped.
推荐答案
p>您可以使用 :
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!");
});
这篇关于如何使用Dart的HttpClient上传PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!