File Upload problems

Mission: FILES MASTER
Task: Upload a file using API

Need help with file upload (Flutter SDK); can’t seem to put ‘file’ in a place where code can find/read it.

Using syntax from File Upload - Backendless SDK for Flutter API Documentation.

Code
ElevatedButton(
onPressed: () async {
String filename = “superfast.html”;
File file = await File(filename).writeAsString(“Hello mbaas!\nUploading files is easy!”);

Backendless.files.upload(file, "mission ").then((response) {
print("File has been uploaded. File URL is - " + response!);
file.delete();
}); // upload
}, // onPressed

Console
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FileSystemException: Cannot open file, path = ‘superfast.html’ (OS Error: Read-only file system, errno = 30)

If ‘filename’ points to ‘assets/files/superfast.html’ (in assets section of pubspec.yaml and in file menu of project), error return changes to:
E/flutter ( 8594): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FileSystemException: Cannot open file, path = ‘assets/files/superfast.html’ (OS Error: No such file or directory, errno = 2)

Hello, @Jim_Austin.

Following code should work:

    String fileName = '/myFile.txt';
    final path = (await getApplicationDocumentsDirectory()).path;
    final file = await io.File(path + fileName).writeAsString('123456');
    var val = await Backendless.files.upload(file, '/a_tests');
    print(val);

P.S. Remember to import the library into your project:
import 'package:path_provider/path_provider.dart';

Best Regards, Nikita.

Thanks Nikita, your code got me there!
Two notes for other newbies:

  • ‘io.File’ refers to dart:io, as in “import ‘dart:io’ as io;”
  • ‘getApplicationDocumentsDirectory’ adds ‘app_flutter’ to the path string; that has to be removed before concatenating ‘filename’

Thanks again, I learned a lot on this one!
Jim

1 Like