Skip to content

Instantly share code, notes, and snippets.

@iamEtornam
Last active July 6, 2020 15:25
Show Gist options
  • Save iamEtornam/8d234922de21424b0eb729de895a61d5 to your computer and use it in GitHub Desktop.
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
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;
}
@iamEtornam
Copy link
Author

Updated on 6th July 2020 based on new concepts and approach i have learnt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment