Created
January 25, 2020 11:03
-
-
Save sankarcheppali/f5a75285c3e873a87a858b45f88accd0 to your computer and use it in GitHub Desktop.
dart file upload
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:path/path.dart'; | |
import 'dart:io'; | |
import 'dart:async'; | |
import 'package:dio/dio.dart'; | |
/** | |
* accepts three parameters, the endpoint, formdata (except fiels),files (key,File) | |
* returns Response from server | |
*/ | |
Future<Response> sendForm( | |
String url, Map<String, dynamic> data, Map<String, File> files) async { | |
Map<String, MultipartFile> fileMap = {}; | |
for (MapEntry fileEntry in files.entries) { | |
File file = fileEntry.value; | |
String fileName = basename(file.path); | |
fileMap[fileEntry.key] = | |
MultipartFile(file.openRead(), await file.length(), filename: fileName); | |
} | |
data.addAll(fileMap); | |
var formData = FormData.fromMap(data); | |
Dio dio = new Dio(); | |
return await dio.post(url, | |
data: formData, options: Options(contentType: 'multipart/form-data')); | |
} | |
/** | |
* accepts two parameters,the endpoint and the file | |
* returns Response from server | |
*/ | |
Future<Response> sendFile(String url, File file) async { | |
Dio dio = new Dio(); | |
var len = await file.length(); | |
var response = await dio.post(url, | |
data: file.openRead(), | |
options: Options(headers: { | |
Headers.contentLengthHeader: len, | |
} // set content-length | |
)); | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to send a data to the server without filename field in the formData