【问题标题】:Send data/pictures to c # api with Dart / Flutter code使用 Dart/Flutter 代码将数据/图片发送到 c# api
【发布时间】:2021-04-04 20:25:21
【问题描述】:

使用 Dart / Flutter,我从图库中选择照片。我怎样才能发送 我选择的照片到 c# api? 我不知道如何将所选图像发送到 api。 感谢您的帮助

Api 会根据 应用。我不明白的部分是;我怎样才能发送照片 到api?

这是我的代码:

import 'dart:io';
        
        import 'package:flutter/material.dart';
        import 'package:image_picker/image_picker.dart';
        
        void main() {
          runApp(new MaterialApp(
            title: "Ymgk Proje",
            home: LandingScreen(),
          ));
        }
        
        class LandingScreen extends StatefulWidget {
          @override
          _LandingScreenState createState() => _LandingScreenState();
        }
        
        class _LandingScreenState extends State<LandingScreen> {
          File imageFile;
        
          _openGallary(BuildContext context) async {
            var picture =
                imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
            this.setState(() {
              imageFile = picture;
            });
            Navigator.of(context).pop();
          }
        
          _openCamera(BuildContext context) async {
            var picture =
                imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
        
            this.setState(() {
              imageFile = picture;
            });
            Navigator.of(context).pop();
          }
        
          Future<void> _showChoiceDiolog(BuildContext context) {
            return showDialog(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    title: Text("Ekleme yönteminizi seçiniz."),
                    content: SingleChildScrollView(
                      child: ListBody(
                        children: <Widget>[
                          GestureDetector(
                            child: Text("Galeri"),
                            onTap: () {
                              _openGallary(context);
                            },
                          ),
                          Padding(padding: EdgeInsets.all(8.0)),
                          GestureDetector(
                            child: Text("Kamera"),
                            onTap: () {
                              _openCamera(context);
                            },
                          )
                        ],
                      ),
                    ),
                  );
                });
          }
        
          Widget _decideImageView() {
            if (imageFile == null) {
              return Text("Henüz resim Seçilmedi!");
            } else {
              return Image.file(imageFile, width: 400, height: 400);
            }
          }
        
          @override
          Widget build(BuildContext context) {
            return Scaffold(
              appBar: AppBar(
                title: Text("YMGK2"),
                centerTitle: mounted,
              ),
              body: Container(
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      _decideImageView(),
                      RaisedButton(
                        onPressed: () {
                          _showChoiceDiolog(context);
                        },
                        child: Text("Resim Ekle"),
                      )
                    ],
                  ),
                ),
              ),
            );
          }
        }

【问题讨论】:

    标签: api flutter dart


    【解决方案1】:

    您可以将File 对象(即imageFile)转换为Uint8List 并将其发送到API。

    File imageFile;
    
    // Your code to get imageFile
    
    
    // Convert imageFile to Uint8List
    Uint8List uint8List = imageFile.readAsBytesSync();
    
    // Upload data to API
    Map<String, dynamic> data = {'imageFile': uint8List};
    
    Response response = await http.post(
      '$baseUrl/api_path',
      body: jsonEncode(
        {
          'reportdata': data,
        },
      ),
      headers: {
        'Content-type': 'application/json',
        'Authorization': '$token',
      },
    );
    return response;
    

    【讨论】:

      猜你喜欢
      • 2018-12-12
      • 2021-03-14
      • 2021-11-08
      • 2021-12-25
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      相关资源
      最近更新 更多