Please help on file uploading using Flutter and Laravel

1,528
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);
}
Share:
1,528
Lutfor Rahman
Author by

Lutfor Rahman

Updated on December 14, 2022

Comments

  • Lutfor Rahman
    Lutfor Rahman over 1 year

    I'm trying to upload images and other different types of files using Flutter mobile sdk and Laravel api. Here is my Flutter code :

    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');
        }
      }
    }
    

    and here is my Laravel code :

    $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);
    
    • Lutfi Ardiansyah
      Lutfi Ardiansyah over 3 years
      is this case solved ? I got this problem on my project too
  • zia sultan
    zia sultan almost 3 years
    I tried this but my server not getting the file showing null.
  • zia sultan
    zia sultan almost 3 years
    please check this link. pastebin.com/KVuYZV3d
  • Guljahan Gurbanmyradowna
    Guljahan Gurbanmyradowna almost 3 years
    I put my code upload image in flutter & laravel in this link, you can check pastebin.com/jdZzMhYd
  • Guljahan Gurbanmyradowna
    Guljahan Gurbanmyradowna almost 3 years
    Please inform me after inspecting this link, or you can contact with me via mail=> [email protected]
  • zia sultan
    zia sultan almost 3 years
    not working message "Call to a member function getClientOriginalExtension() on null, " this is the error I'm getting