Skip to content

Instantly share code, notes, and snippets.

@thetrav
Last active June 10, 2020 20:22
Show Gist options
  • Save thetrav/eef0770f79ec29b682616fa63faae6fe to your computer and use it in GitHub Desktop.
Save thetrav/eef0770f79ec29b682616fa63faae6fe to your computer and use it in GitHub Desktop.
Upload file to server
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
Future<http.Response> uploadFile(
String url,
Map<String, String> headers,
Map<String, String> fields,
String fileName,
List<int> fileBytes) async {
var request = new http.MultipartRequest("POST", Uri.parse(url));
request.files.add(http.MultipartFile.fromBytes(
'file',
fileBytes,
contentType: MediaType('application', 'octet-stream'),
filename: fileName));
fields.forEach((k, v) => request.fields[k] = v);
request.headers.addAll(headers);
var streamedResponse = await request.send();
return await http.Response.fromStream(streamedResponse);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment