From 7bfc26b5a05d53b12d358e07cf8a9cf3e6d6fcfc Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 25 May 2020 22:21:45 +1000 Subject: [PATCH] Function to upload file via API --- lib/api.dart | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/api.dart b/lib/api.dart index 62d875ee..0476f601 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -276,6 +276,30 @@ class InvenTreeAPI { ); } + /* + * Upload a file to the given URL + */ + Future uploadFile(String url, File f, {String name = "attachment", Map fields}) async { + var _url = makeApiUrl(url); + + var request = http.MultipartRequest('POST', Uri.parse(_url)); + + request.headers.addAll(defaultHeaders()); + + fields.forEach((String key, String value) { + request.fields[key] = value; + }); + + var _file = await http.MultipartFile.fromPath(name, f.path); + + request.files.add(_file); + + var response = await request.send(); + + return response; + + } + // Perform a POST request Future post(String url, {Map body}) async { @@ -322,7 +346,6 @@ class InvenTreeAPI { var headers = Map(); headers[HttpHeaders.authorizationHeader] = _authorizationHeader(); - //headers['Authorization'] = _authorizationHeader(); return headers; }