Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
Http 将图像上载到后端_Http_Flutter_Dart - Fatal编程技术网

Http 将图像上载到后端

Http 将图像上载到后端,http,flutter,dart,Http,Flutter,Dart,我无法通过http包将图像上载到我的restAPI服务器。我花了一整天寻找答案,但没有找到任何答案。我的后端需要一个实际的映像,但我似乎不使用http包来管理它 Future<void> createProfile(Profile profile) async { try { var request = new http.MultipartRequest("POST", Uri.parse(APIPath.createProfile()))

我无法通过http包将图像上载到我的restAPI服务器。我花了一整天寻找答案,但没有找到任何答案。我的后端需要一个实际的映像,但我似乎不使用http包来管理它

  Future<void> createProfile(Profile profile) async {
    try {
      var request =
          new http.MultipartRequest("POST", Uri.parse(APIPath.createProfile()));
      request.fields.addAll(profile.toMap());
      request.files.add(await http.MultipartFile.fromPath(
          'image', profile.image.path,
          contentType: MediaType('image', '*')));
      request.headers['authorization'] = "Bearer $_token";
      final response = await request.send();
      if (response.statusCode == 200) print('Uploaded!');

      notifyListeners();
    } catch (error) {
      throw error;
    }
  }

这是因为
contentType:MediaType('image','*')
将导致
image/*
,它与
node.js
中的
文件.mimetype
不匹配
request.files.add(等待http.MultipartFile.fromPath('image',profile.image.path,)
是否可以在
node.js
中打印出
file.mimetype
?@JohnJoe-hmm思维良好
image/*
@JohnJoe我已经将
MediaType('image','*')
更改为
MediaType('image','jpg')
,并且可以正常工作。我使用
“*”
的原因是允许将客户端的所有图像类型上载到后端。。那个么,你们如何允许上传所有的图像类型而不指定类型呢?@JohnJoe,没关系。我会问一个新问题。你帮了我很多。。谢谢你能回答这个问题吗?你指的是我犯的错误,所以我可以把这个问题标记为已回答?谢谢你的回答,但这正是我的答案,它不起作用。请参阅我的问题中的更新代码。虽然这段代码可能会解决这个问题,但如何以及为什么解决这个问题会真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
 uri = Uri.parse(url);

 var request = new MultipartRequest("PUT", uri);

 var multipartFile = await MultipartFile.fromPath("package", path);

 //path = filepath 

 request.files.add(multipartFile);

 StreamedResponse response = await request.send();

 return response;
 uri = Uri.parse(url);

 var request = new MultipartRequest("PUT", uri);

 var multipartFile = await MultipartFile.fromPath("package", path);

 //path = filepath 

 request.files.add(multipartFile);

 StreamedResponse response = await request.send();

 return response;