Last active
July 6, 2020 15:25
-
-
Save iamEtornam/8d234922de21424b0eb729de895a61d5 to your computer and use it in GitHub Desktop.
a simple function for uploading image to a server and returning a link to that image in dart/flutter
This file contains hidden or 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 'dart:io'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:http/http.dart' as http; | |
import 'package:http_parser/http_parser.dart'; | |
import 'package:path/path.dart'; | |
Future<http.Response> imageUploadRequest({@required File imageFile}) async { | |
Map<String, String> headers = {"Accept": "application/json"}; | |
final multipartRequest = | |
new http.MultipartRequest('POST', Uri.parse('<END_POINT_TO_UPLOAD>')); | |
multipartRequest.headers.addAll(headers); | |
String fileName = basename(imageFile.path); | |
multipartRequest.files.add( | |
await http.MultipartFile.fromPath('image', imageFile.path, | |
filename: fileName, contentType: MediaType("image", "png")), | |
); | |
http.Response response = | |
await http.Response.fromStream(await multipartRequest.send()) | |
.timeout(Duration(seconds: 120)); //2 MINUTES TIMEOUT | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated on 6th July 2020 based on new concepts and approach i have learnt.