Skip to content

Instantly share code, notes, and snippets.

@emiliodallatorre
Created October 4, 2020 15:43
Show Gist options
  • Save emiliodallatorre/8772263a290bc30f592bc9cf27872c48 to your computer and use it in GitHub Desktop.
Save emiliodallatorre/8772263a290bc30f592bc9cf27872c48 to your computer and use it in GitHub Desktop.
Funzione per caricare un file via http con Flutter.
import 'package:http/http.dart' as http;
class HttpUploader {
Future<void> uploadFile (File file, String nameOnPrinter) async {
String url = "http://192.168.1.125/upload?X-Filename=$nameOnPrinter.gcode";
Map<String, String> headers = {
"Content-Type": "application/octet-stream"
};
// Qui otteniamo un formato di file più facile da trasmettere.
Uint8List fileAsBytes = file.readAsBytesSync();
// Qui effettivamente il file viene trasmesso, e si ottiene una risposta dalla stampante.
http.Response response = await http.post(
url,
headers: headers,
body: bytes,
);
debugPrint(response.body);
debugPrint(response.statusCode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment