postman 上传后在json响应中不显示图片类型[Flutter Web]

zujrkrfu  于 11个月前  发布在  Postman
关注(0)|答案(1)|浏览(130)

最近在我的Flutter Web应用程序项目中,我需要上传一个图像到服务器,我遇到了很多问题,所以我终于找到了一种方法,但在将图像上传到服务器后,它不会在JSON响应中显示图像类型。然而,我知道在后端上传是正确的,而且在上传图像后,在服务器端显示正确,但在JSON响应中不正确。
如果你看黄色的一面,一切都是正确的,是由 Postman 上传的。但如果你看绿色的一面是不正确的,这是由Flutter网络应用程序上传的。
x1c 0d1x的数据
我的Flutter代码图片选择:

List<int>? _selectedFile;
  Uint8List? _bytesData;

  startWebFilePicker() async {
    FileUploadInputElement uploadInput = FileUploadInputElement();
    uploadInput.multiple = true;
    uploadInput.draggable = false;
    uploadInput.click();

    uploadInput.onChange.listen((event) {
      final files = uploadInput.files;
      final file = files![0];
      final reader = FileReader();

      reader.onLoadEnd.listen((event) {
        setState(() {
          _bytesData =
              Base64Decoder().convert(reader.result.toString().split(",").last);
          _selectedFile = _bytesData;
          print('_bytesData -> ${_bytesData}');
        });
      });
      reader.readAsDataUrl(file);
    });
  }

字符串
上传到服务器:

Future uploadImage() async {
    String token = await apiController.getToken();
    var url = Uri.parse(conf.url);
    var request = http.MultipartRequest("POST", url);
    request.headers['Authorization'] = 'Bearer $token';
    request.headers['Content-Type'] = 'multipart/form-data';

    request.fields['name'] = 'parent cat';
    request.fields['description'] = 'description cat';
    request.fields['parent_id'] = '';
    request.fields['is_active'] = '1';
    request.files.add(await http.MultipartFile.fromBytes(
        'image', _selectedFile!,
        contentType: new MediaType('*', '*'), filename: "image"));

    request.send().then((response) {
      if (response.statusCode == 200) {
        print('response.statusCode -> ${response.stream}');
        print("File uploaded successfully");
      } else {
        print('file upload failed');
      }
    });
  }


上传后为我记录cat响应->文件上传成功
我不知道该怎么办,因为一切都是正确的,我从服务器得到正确的响应,但照片没有扩展。

icnyk63a

icnyk63a1#

您需要上传图像与他们的名字沿着与他们的扩展名。例如。image1.jpg,image2.jpg

Future uploadImage() async {
    String token = await apiController.getToken();
    var url = Uri.parse(conf.url);
    var request = http.MultipartRequest("POST", url);
    request.headers['Authorization'] = 'Bearer $token';
    request.headers['Content-Type'] = 'multipart/form-data';

    request.fields['name'] = 'category name'; 

    request.fields['description'] = 'description cat';
    request.fields['parent_id'] = '';
    request.fields['is_active'] = '1';
    request.files.add(await http.MultipartFile.fromBytes(
        'image', _selectedFile!,
        contentType: new MediaType('*', '*'), filename: "image.jpg"))//Add your file name here;

    request.send().then((response) {
      if (response.statusCode == 200) {
        print('response.statusCode -> ${response.stream}');
        print("File uploaded successfully");
      } else {
        print('file upload failed');
      }
    });
  }

字符串

相关问题