donglun4521 2019-08-06 08:57
浏览 251

使用base64Encode将图像上传到服务器失败

I use image picker to get photo from camera then upload it to the server with base64Encode, like this

http.post( api('file/update_pic_item/2'), body: data, headers: {HttpHeaders.authorizationHeader: prefs.getString('token'), "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}).then((response) async {
      toast('Success');
      Map res = json.decode(response.body); print(res);
});

By server I got this message

The pic1 must be an image., The pic1 must be a file of type: jpeg, jpg, bmp, png

I use lumen for my backend,

$this->validate($request, [
            'pic1' => 'nullable|image|mimes:jpeg,jpg,bmp,png|max:10240', ]);
        $upload_path = 'images/items';

        if ($request->hasFile('pic1')) {
            $pic1 = $request->file('pic1');
            $ext1 = $pic1->getClientOriginalExtension();
            if ($pic1->isValid()) {
                $pic_name = Carbon::now()->format('YmdHs') . "a." . $ext1;
                $pic1->move($upload_path, $pic_name);
                $item->update(['pic1' => $pic_name]);
            }
        }

How to solve this problem? thank you so much for your help

  • 写回答

1条回答 默认 最新

  • doudou7361 2019-08-06 09:03
    关注

    Use this code to upload image on server

    Future upload(File imageFile)async{
          var stream= new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
          var length= await imageFile.length();
          var uri = Uri.parse('imageUrl');
          var request = new http.MultipartRequest("POST", uri);
          var imageUri = 'salati_${widget.user.user_id}_${DateTime.now().millisecondsSinceEpoch}'+imageFile.path.substring(imageFile.path.lastIndexOf("."));
          var multipartFile = new http.MultipartFile("image", stream, length, filename: basename(imageUri));
          request.files.add(multipartFile);
          var response = await request.send();
          if(response.statusCode==200){
            print("Image Uploaded");
          }else{
            print("Upload Failed");
          }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题