Flutter/Dart: 如何上传二进制文件到Dropbox?
Flutter/Dart: How upload binary file to Dropbox?
我正在尝试使用 https://github.com/dart-lang/http. Following https://dropbox.github.io/dropbox-api-v2-explorer/#files_upload 将二进制文件上传到 Dropbox,我的代码是:
import 'package:http/http.dart' as http;
Map<String,String> headers = {
'Authorization': 'Bearer MY_TOKEN',
'Content-type': 'application/octet-stream',
'Dropbox-API-Arg': '{"path": "/file", "mode": "overwrite"}',
};
final resp = await http.post(
Uri.parse("https://content.dropboxapi.com/2/files/upload"),
body: "--data-binary @\'/my/binary/file\'",
headers: headers);
file
已上传,但不幸的是,这仅包含文本 --data-binary @'/my/binary/file'
(即不是实际文件)。
我是不是漏掉了something/doing什么东西?
既然你通过了
body: "--data-binary @\'/my/binary/file\'",
作为您的 POST 请求的正文,这正是要发送到服务器的数据。正文是原始数据,只是传递到服务器,不会被 dart 解释。
看看 Dart 的 MultipartRequest. Or for general information:SO on multipart requests。
正文工作时直接发送二进制数据:
import 'package:http/http.dart' as http;
final file = File('/my/binary/file');
Uint8List data = await file.readAsBytes();
Map<String,String> headers = {
'Authorization': 'Bearer MY_TOKEN',
'Content-type': 'application/octet-stream',
'Dropbox-API-Arg': '{"path": "/file", "mode": "overwrite"}',
};
final resp = await http.post(
Uri.parse("https://content.dropboxapi.com/2/files/upload"),
body: data,
headers: headers);
我正在尝试使用 https://github.com/dart-lang/http. Following https://dropbox.github.io/dropbox-api-v2-explorer/#files_upload 将二进制文件上传到 Dropbox,我的代码是:
import 'package:http/http.dart' as http;
Map<String,String> headers = {
'Authorization': 'Bearer MY_TOKEN',
'Content-type': 'application/octet-stream',
'Dropbox-API-Arg': '{"path": "/file", "mode": "overwrite"}',
};
final resp = await http.post(
Uri.parse("https://content.dropboxapi.com/2/files/upload"),
body: "--data-binary @\'/my/binary/file\'",
headers: headers);
file
已上传,但不幸的是,这仅包含文本 --data-binary @'/my/binary/file'
(即不是实际文件)。
我是不是漏掉了something/doing什么东西?
既然你通过了
body: "--data-binary @\'/my/binary/file\'",
作为您的 POST 请求的正文,这正是要发送到服务器的数据。正文是原始数据,只是传递到服务器,不会被 dart 解释。
看看 Dart 的 MultipartRequest. Or for general information:SO on multipart requests。
正文工作时直接发送二进制数据:
import 'package:http/http.dart' as http;
final file = File('/my/binary/file');
Uint8List data = await file.readAsBytes();
Map<String,String> headers = {
'Authorization': 'Bearer MY_TOKEN',
'Content-type': 'application/octet-stream',
'Dropbox-API-Arg': '{"path": "/file", "mode": "overwrite"}',
};
final resp = await http.post(
Uri.parse("https://content.dropboxapi.com/2/files/upload"),
body: data,
headers: headers);