无法在抖动中使用dio将图片上传到api

时间:2020-07-08 17:38:20

标签: node.js api flutter dio

我已经使用此代码从移动设备中的图库中选择图像,然后使用api将其与其他数据一起上传到服务器,其他一切正常,除图像外,所有数据均已成功上传,我不知道这是什么问题,尤其是使用Postman时,即使图像已成功上传...这也是我在颤抖侧的代码:

TextEditingController  namec = TextEditingController();
  TextEditingController  descc = TextEditingController();
  TextEditingController  pricec = TextEditingController();
   File image;
   String imgUrl;


   getImage() async{
     var imageIns =  await ImagePicker().getImage(source: ImageSource.gallery);
     setState(() {
       image = File(imageIns.path);
       imgUrl = imageIns.path;
     });
   }
   /////////////////////
  uploadImage() async {
    Dio dio = new Dio();
    FormData formData = new FormData.fromMap({
      "name" : namec.text,
      "file" : image,
      "desc" : descc.text,
      "price" : pricec.text,
      "cat" : widget.cid
    });
    var res = await dio.post(
        "http://192.168.43.106:3000/service", data: formData);
    final data = json.decode(res.data);

,这在node.js端(api)中用于插入包含图像的数据:

insertServices: async (req, res)=>{
   // var path = req.file.path.replace(/\\/g, '/');
    const result = await new SERVICES({
    name : req.body.name,
    file :  req.file != null ? req.file.path.replace(/\\/g, '/') : "PATH",
    desc : req.body.desc,
    price : req.body.price,
    cat : req.body.cat
    }).save()
    res.json({
        message: "success",
        id : result._id,
        name : result.name,
        file : result.file,
        desc : result.desc,
        price : result.price
    })
    },

  and i am using (multer) middle ware to upload the image, 

这是服务器端的错误,当我尝试通过flutter应用程序上传图片时(没有邮递错误):

::ffff:192.168.43.1 - - [08/Jul/2020:11:43:28 +0000] "GET /PATH HTTP/1.1" 404 143
(node:15612) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'path' of undefined
    at insertServices (G:\eservices\logic\service.js:67:22)
    at Layer.handle [as handle_request] (G:\eservices\node_modules\express\lib\router\layer.js:95:5)
    at next (G:\eservices\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (G:\eservices\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (G:\eservices\node_modules\express\lib\router\layer.js:95:5)
    at G:\eservices\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (G:\eservices\node_modules\express\lib\router\index.js:335:12)
    at next (G:\eservices\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (G:\eservices\node_modules\express\lib\router\index.js:174:3)
    at router (G:\eservices\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (G:\eservices\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (G:\eservices\node_modules\express\lib\router\index.js:317:13)
    at G:\eservices\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (G:\eservices\node_modules\express\lib\router\index.js:335:12)
    at next (G:\eservices\node_modules\express\lib\router\index.js:275:10)
    at compression (G:\eservices\node_modules\compression\index.js:220:5)
(node:15612) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15612) [DEP0018] DeprecationWa

1 个答案:

答案 0 :(得分:0)

我有一个类似的应用程序可以从画廊上传图像,这是一个示例:

List<Asset> images = List<Asset>();

我使用multi_image_picker一次选择多张图像,并在填充images列表后使用以下代码上传它们:

Dio dio = new Dio();
  var items = [];
  for (var item in images) {
  ByteData byteData = await item.getThumbByteData(800, 800);
  List<int> imageData = byteData.buffer.asUint8List();

  MultipartFile i =
  MultipartFile.fromBytes(
    imageData,
    filename: item.name,
  );
  items.add(i);
}
      
FormData formData = new FormData.fromMap({"userfile": items});
Response response = await dio.post(phpEndPoint, data: formData);
if(response.statusCode==200) //success

不幸的是,我不确定后端中包含什么代码。