appwrite / sdk-for-dart

[READ-ONLY] Official Appwrite Dart SDK πŸ’™
https://appwrite.io
BSD 3-Clause "New" or "Revised" License
113 stars 17 forks source link

storage.createFile not working #14

Closed pretorean closed 2 years ago

pretorean commented 2 years ago

πŸ‘Ÿ Reproduction steps

I am trying to upload a file using the code from the example and I get an error. I tried uploading a text file and a picture (commented out in the code) Maybe I'm doing something wrong?

my code:

void main(List<String> arguments) async {
  Client client = Client()
    ..setEndpoint(endpoint)
    ..setProject(projectId)
    ..setKey(secretKey)
    ..setSelfSigned();
  Storage storage = Storage(client);

  final content = 'some text file\nmultiline file';
  final file = MultipartFile.fromString('field', content, filename: 'test_text_file.txt', );

  // final file = await MultipartFile.fromPath(
  //   'image.jpg',
  //   './path-to-file/image.jpg',
  //   filename: 'image.jpg',
  //   contentType: MediaType.parse('image/jpeg'),
  // );

  storage.createFile(file: file).then((response) {
    print(response); // File uploaded!
  }).catchError((error) {
    print(error.toString());
    print(error.response);
  });
}
name: appwrite_test
description: A simple command-line application.
version: 1.0.0

environment:
  sdk: '>=2.14.4 <3.0.0'

dependencies:
  dart_appwrite: ^1.0.2

dev_dependencies:
  lints: ^1.0.1

πŸ‘ Expected behavior

the file should be uploaded to the server

πŸ‘Ž Actual Behavior

I see in console:

AppwriteException: No file sent (400)
{message: No file sent, code: 400, version: 0.11.0}

🎲 Appwrite version

Version 0.10.x

πŸ’» Operating system

MacOS

🧱 Your Environment

Dart SDK version: 2.14.4 (stable) (Wed Oct 13 11:11:32 2021 +0200) on "macos_x64"

πŸ‘€ Have you spent some time to check if this issue has been raised before?

🏒 Have you read the Code of Conduct?

stnguyen90 commented 2 years ago

This:

  final file = MultipartFile.fromString('field', content, filename: 'test_text_file.txt', );

sets the Multipart field name to be field but the server expects it to be file.

Similarly, image.jpg should be file in the following:

 // final file = await MultipartFile.fromPath(
  //   'image.jpg',
  //   './path-to-file/image.jpg',
  //   filename: 'image.jpg',
  //   contentType: MediaType.parse('image/jpeg'),
  // );
pretorean commented 2 years ago

Thanks it works!