Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart:http post上传到谷歌硬盘_Dart_Flutter - Fatal编程技术网

Dart:http post上传到谷歌硬盘

Dart:http post上传到谷歌硬盘,dart,flutter,Dart,Flutter,在使用命令行curl命令成功上载文件后,我尝试将文件上载到Dart中的google Drive: curl -X POST -L \ -H "Authorization: Bearer $access_token" \ -F "metadata={name : 'test_send_file.zip'};type=application/json;charset=UTF-8" \ -F "file=@test_send_file.zip;type=application/

在使用命令行curl命令成功上载文件后,我尝试将文件上载到Dart中的google Drive:

curl -X POST -L \
    -H "Authorization: Bearer $access_token" \
    -F "metadata={name : 'test_send_file.zip'};type=application/json;charset=UTF-8" \
    -F "file=@test_send_file.zip;type=application/zip" \
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"

dart版本:

import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';

final access_token = "....";

main() async {
  File first = File('test_send_file.zip');
  Uri uri = Uri.parse(
      'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart');

  http.MultipartRequest request = new http.MultipartRequest('POST', uri);

  request.headers["Authorization"] = "Bearer $access_token";
  request.fields['metadata'] =
      "{name : test_send_file.zip};type=application/json;charset=UTF-8";
  request.files.add(await http.MultipartFile.fromPath('test_send_file.zip', first.path,
      contentType: new MediaType('application', 'zip')));
  print("request.toString: " + request.toString());
  http.StreamedResponse response = await request.send();
  print(response.statusCode);
}

我收到一个状态代码错误请求:
400


有人能帮我向google drive in Dart发送一个文件http请求吗?

当你想向
多部分请求添加一个更复杂的表单项时,你可以将其作为文件添加。(是的,有点奇怪……)

替换
请求.字段。将…
添加为

  request.files.add(http.MultipartFile.fromString(
    'metadata',
    json.encode({'name': 'test_send_file.zip'}),
    contentType: MediaType('application', 'json'),
  ));

谢谢你,这很有效!我已将
request.fields['metadata']..
request.files.add…
替换为
request.files.add.
已被此问题困扰。这是行不通的。这个名字总是没有名字。似乎元数据需要作为多部分/相关内容类型传递,但我得到了400