Skip to content

Instantly share code, notes, and snippets.

@mfadiilahrio
Created March 2, 2020 08:52
Show Gist options
  • Save mfadiilahrio/f326e8596984efd42a36ba374dcee49a to your computer and use it in GitHub Desktop.
Save mfadiilahrio/f326e8596984efd42a36ba374dcee49a to your computer and use it in GitHub Desktop.
import 'dart:io';
import 'package:http/http.dart';
class Upload {
final Client client;
Upload({this.client});
Future execute(Uri yourFileUri) async {
final uri = "https://your_end_point.com/upload"; // URL ENDPOINT KAMU
final mimeType = "image/jpeg"; // JENIS FILE YANG AKAN DIKIRIM, KALI INI KITA HARDCODE DULU
Map<String, String> uploadHeaders = {'Content-type': mimeType}; // HEADER YANG ENDPOINT KAMU BUTUHIN
var uploadResponse = await client.put(Uri.parse(uri), // REQUEST METHODNYA SESUAIIN SAMA ENDPOINT KAMU, ANE PAKE PUT
headers: uploadHeaders,
body: await File.fromUri(yourFileUri).readAsBytes()); // FILE YANG DIKIRIM DIRUBAH JADI BYTES
if (uploadResponse.statusCode == 200) {
print("200");
} else {
print("Error ${uploadResponse.statusCode}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment