【问题标题】:Please help on file uploading using Flutter and Laravel请帮助使用 Flutter 和 Laravel 上传文件
【发布时间】:2020-01-15 13:09:46
【问题描述】:

我正在尝试使用 Flutter mobile sdk 和 Laravel api 上传图片和其他不同类型的文件。这是我的颤振代码:

class _MyHomePageState extends State<MyHomePage> {
  File _image;

  Future getImageGallery() async {
    var imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      _image = imageFile;
    });
  }

  Future getImageCamera() async {
    var imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
    setState(() {
      _image = imageFile;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Center(
              child: _image == null
                  ? Text('No image selected')
                  : Image.file(_image),
            ),
            RaisedButton(
              child: Icon(Icons.image),
              onPressed: getImageGallery,
            ),
            RaisedButton(
              child: Icon(Icons.camera_alt),
              onPressed: getImageCamera,
            ),
            RaisedButton(
              child: Icon(Icons.file_upload),
              onPressed: () {
                upload(_image);
              },
            ),
          ],
        ),
      ),
    );
  }


  Future upload(File imageFile) async {
    print(imageFile.path);
    var stream = http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
    var length = await imageFile.length();
    var uri = Uri.parse('https://api.tredlr.com/api/upload');
    var request = http.MultipartRequest('POST', uri);
    var multipartFile = http.MultipartFile('image', stream, length,
        filename: basename(imageFile.path));
    request.files.add(multipartFile);
    var response = await request.send();

    print(response);
    print(response.stream);
    print(response.statusCode);

    if (response.statusCode == 200) {
      print('uploaded');
    } else {
      print('not uploaded');
    }
  }
}

这是我的 Laravel 代码:

$photo = $request->file("image");
$ext = $photo->getClientOriginalExtension();
$fileName = rand(10000, 50000) . '.' .$ext;

$thumbSm = 'thumb_sm' . rand(10000, 50000) . '.' .$ext;

$image = Image::make($request->file('image'));
$image->save(base_path().'/public/'. $fileName);
$image->resize(120, 120);
$image->save(base_path().'/public/'. $thumbSm);

【问题讨论】:

  • 这个案子解决了吗?我的项目也遇到了这个问题

标签: laravel file file-upload flutter


【解决方案1】:
Future _test(File file) async {
    Dio dio = new Dio();
    file.existsSync();
    String fileName = file.path.split('/').last;
    FormData formData = new FormData.fromMap({
        "image": await MultipartFile.fromFile(file.path,filename: fileName)
    });
    response = await dio.post("http://you ip address/api/route", data: formData);
}

【讨论】:

  • 我试过了,但我的服务器没有得到显示为空的文件。
  • 请查看此链接。 pastebin.com/KVuYZV3d
  • 我把我的代码上传图片放在flutter & laravel这个链接里,你可以查看pastebin.com/jdZzMhYd
  • 请在查看此链接后通知我,或者您可以通过邮件与我联系=> gurbanmyradownaguljahan@gmail.com
  • 不工作消息“在 null 上调用成员函数 getClientOriginalExtension(),”这是我遇到的错误
猜你喜欢
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 2011-11-14
  • 1970-01-01
  • 2011-06-29
  • 2016-05-12
  • 1970-01-01
相关资源
最近更新 更多