我正在嘗試使用https://github.com/dart-lang/http將二進制檔案上傳到 Dropbox 。在https://dropbox.github.io/dropbox-api-v2-explorer/#files_upload之后,我的代碼是:
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'(即不是實際檔案)。
我錯過了什么/做錯了什么嗎?
uj5u.com熱心網友回復:
既然你通過
body: "--data-binary @\'/my/binary/file\'",
作為您的 POST 請求的主體,這正是要發送到服務器的資料。body 是原始資料,只是傳遞給服務器,dart 不會解釋。
看看 Dart 的MultipartRequest。或者對于一般資訊:SO on multipart requests。
uj5u.com熱心網友回復:
在正文作業時直接發送二進制資料:
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://www.uj5u.com/caozuo/463631.html
