Last active
June 10, 2020 20:22
-
-
Save thetrav/eef0770f79ec29b682616fa63faae6fe to your computer and use it in GitHub Desktop.
Upload file to server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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