0

I try to upload image to Firebase local storage emulator from flutter desktop software. I'm using file_selector_windows plugin for file selection.

void _openImageFile() async {
  final typeGroup = XTypeGroup(
    label: 'images',
    extensions: ['jpg', 'png'],
  );
  final files = await FileSelectorPlatform.instance
      .openFiles(acceptedTypeGroups: [typeGroup]);
  final file = files[0];
  final fileName = file.name;
  final filePath = file.path;
  final responseFireStorage = await http.post(
    Uri.parse('http://localhost:9199/v0/b/default-bucket/o?name=sample.jpg'),
    headers: <String, String>{
      'Content-Type': 'image/jpeg',
    },
    body: filePath,
  );
  print(responseFireStorage.statusCode);
}

Error: 400 BadRequest

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You are telling the server you will send an `image/jpeg`, yet you are just sending a string (the path of the file) ... You may need to add the file content to the post request ... – derpirscher Jul 03 '21 at 14:22
  • @derpirscher i tried file.readAsBytes(). but still error Bad request. Thanks for response. – Slate and Chalk Jul 04 '21 at 18:27
  • @derpirscher tried this also solution from [Stack overflow](https://stackoverflow.com/questions/49125191/how-to-upload-images-and-file-to-a-server-in-flutter). file uploaded sucessfully but i got only dot pixel. check this link [my firebase storage](https://firebasestorage.googleapis.com/v0/b/coun-ab246.appspot.com/o/y.jpg?alt=media&token=f68197df-eba2-4de2-9de8-bd5207e1be03) – Slate and Chalk Jul 04 '21 at 19:05
  • @derpirscher `final responseFireStore = await http.post( Uri.parse( 'https://firebasestorage.googleapis.com/v0/b/coun-ab246.appspot.com/o?name=avatar.jpg'), headers: { 'Content-Type': 'image/jpeg', }, body: File(filePath).readAsBytesSync().buffer.asUint8List(), );` i tried this working well, i got picture in firebase storage. this not working in firebase local emulator showing bad request. – Slate and Chalk Jul 05 '21 at 08:18
  • Well, this may be an issue with the emulator then, if it's behaving differently from the firebase server. I never used either of them, so I can't really help you there. My hint was just the obvious inconsistency in your original request. – derpirscher Jul 05 '21 at 08:23

0 Answers0