Skip to content

Commit 7ba21f6

Browse files
authored
Added function to upload Media files to WordPress
Added functionality to upload Media files to Wordpress Media Library
1 parent 32b4a59 commit 7ba21f6

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

lib/flutter_wordpress.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,34 @@ class WordPress {
652652
}
653653

654654
// yahya - @mymakarim
655+
656+
async.Future<dynamic> uploadMedia(File image) async {
657+
final StringBuffer url = new StringBuffer(_baseUrl + URL_MEDIA);
658+
var file = image.readAsBytesSync();
659+
final response = await http.post(
660+
url.toString(),
661+
headers: {
662+
"Content-Type": "image/png",
663+
"Content-Disposition": "form-data; filename=firstIg.png",
664+
"Authorization": "${_urlHeader['Authorization']}"
665+
},
666+
body: file,
667+
);
668+
669+
if (response.statusCode >= 200 && response.statusCode < 300) {
670+
return json.decode(response.body);
671+
} else {
672+
try {
673+
WordPressError err =
674+
WordPressError.fromJson(json.decode(response.body));
675+
throw err;
676+
} catch (e) {
677+
throw new WordPressError(message: response.body);
678+
}
679+
}
680+
}
681+
682+
// uploadMedia function added by: @GarvMaggu
655683

656684
async.Future<bool> createUser({@required User user}) async {
657685
final StringBuffer url = new StringBuffer(_baseUrl + URL_USERS);

0 commit comments

Comments
 (0)