Skip to content

Commit

Permalink
Merge pull request #4 from brutalzinn/feature/add-multifile-upload
Browse files Browse the repository at this point in the history
add support to multi upload files
  • Loading branch information
PauloLeal committed Nov 7, 2023
2 parents d17aa3b + 748b64f commit 8d65fd8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/utils/http_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ class HttpUtils {
headers: headers, bytes: bytes, filename: filename);
}

static Future<HttpResponse> postFiles(
String url,
Map<String, Uint8List> files, {
Map<String, String>? headers,
}) async {
return _reqJson("multi_upload", url, headers: headers, files: files);
}

static String _hashRequest(
String method,
String url, {
Expand Down Expand Up @@ -118,6 +126,7 @@ class HttpUtils {
bool cached = false,
bool hasLog = false,
Uint8List? bytes,
Map<String, Uint8List>? files,
}) async {
SharedPreferencesService ls = SharedPreferencesService.instance;
headers ??= {};
Expand Down Expand Up @@ -159,6 +168,15 @@ class HttpUtils {
http.MultipartFile.fromBytes('fileUpload', bytes!, filename: filename),
);
response = await http.Response.fromStream(await request.send());
} else if (method == "multi_upload") {
var request = http.MultipartRequest('POST', Uri.parse(url));
request.headers.addAll(headers);
files!.forEach((key, value) {
request.files.add(
http.MultipartFile.fromBytes('fileUpload', value, filename: key),
);
});
response = await http.Response.fromStream(await request.send());
} else {
if (method == "get") {
response = hasLog
Expand Down
14 changes: 13 additions & 1 deletion test/http_util_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:ftbase/ftbase.dart';
import 'package:test/test.dart';
Expand All @@ -7,6 +8,17 @@ void main() {
test('Upload File', () async {
var filename = '/Users/renanjunior/Documents/GitHub/ftbase/test/teste.csv';

await HttpUtils.postFile('http://127.0.0.1:3000/config/upload', File(filename).readAsBytesSync(), "teste");
await HttpUtils.postFile('http://127.0.0.1:3000/config/upload',
File(filename).readAsBytesSync(), "teste");
});

test('Upload multi Files', () async {
var filename = '/Applications/doc.pdf';

Map<String, Uint8List> files = {
"fileName1": File(filename).readAsBytesSync(),
"fileName2": File(filename).readAsBytesSync()
};
await HttpUtils.postFiles('http://127.0.0.1:3000/config/upload', files);
});
}

0 comments on commit 8d65fd8

Please sign in to comment.