I am using the POST method to upload video file from Flutter App using the Dio package. Vimeo's POST method first generates a link for us to upload the video to. The video is uploaded successfully most of the time. Every once in a while, it fails (like 1/10).
To Test if this is an issue on Vimeo's server I tried uploading the video from POSTMAN, 20/20 (all of them) times it uploaded successfully. So I am assuming there is something wrong with the DIO Package?
I have also posted this issue in the DIO's github issues:
https://github.com/flutterchina/dio/issues/1535#issue-1328014439
Upload video code:
if (url != null) {
print('upload link is not null!');
Strings.videoURL = url;
print('upload url is <>:');
print(Strings.videoURL);
try {
Dio uploadVideoDio = new Dio();
uploadVideoDio.options.headers["authorization"] = dotenv.env['VIMEO_ACCESS_TOKEN'];
uploadVideoDio.options.headers["content-type"] = "application/json";
uploadVideoDio.options.headers["accept"] = "application/json";
var formData = FormData.fromMap({
"file_data": await MultipartFile.fromFile(file.path)
});
await uploadVideoDio.post(
Strings.videoURL,
data: formData,
options: Options(
followRedirects: false,
responseType: ResponseType.json,
validateStatus: (status) {
print('status is :');
print(status);
// return status! < 500;
if (status == 302) {
uploadStatus = 'successful';
return true;
} else {
return false;
}
}),
);
print('file upload called');
print('upload status is ');
print(uploadStatus);
return uploadStatus;
} on DioError catch (err) {
print('error uploading actual video');
print(err.toString());
return uploadStatus;
}
} else {
print('upload link is null');
return uploadStatus;
}
Here's an update
I got in touch with the Vimeo's support team and they are investigating the issue. Their response is that
The "POST" method is not very reliable and you should use the TUS
resumable approach.
I am about to test the stability of the TUS method and will update this post afterwards
These 2 methods take a global file that is set later on and send it to OCR Space API to convert and send back a OCR PDF of the file.
The code is running well, and the output value is the OCR correctly, but it is not receiving the the URL of isCreateSearchablePdf (as stated by the OCR Space API)
Is this an issue of emulator? Or is printing value wrong?
OCRSPACEAPI website: https://ocr.space/ocrapi
void fOCR(File file) async {
try {
var uri = Uri.parse('https://api.ocr.space/parse/image?');
var request = new http.MultipartRequest("POST", uri);
request.fields["apikey"] = "myApiKey";
request.fields["language"] = "ara";
request.fields["isOverlayRequired"] = "true";
request.fields["isCreateSearchablePdf"] = "true";
http.MultipartFile multipartFile = await http.MultipartFile.fromPath(
'pdf',
file.path
);
request.files.add(multipartFile);
await request.send().then((response) async {
print("Result: ${response.statusCode}");
print(value);
});
}).catchError((e) {
print(e);
});
} catch(e){
print(e);
}
}
Future selectFile() async {
final result = await FilePicker.platform.pickFiles(allowMultiple: false);
if (result == null) return;
final path = result.files.single.path!;
print(result);
print(File(path));
setState(() => file = File(path));
enabled = true;
}
In my app I am downloading a files from server. I need to save this files to downloads folder in mobile phone storage. Can this was possible using path_provider package in android ?
This might be a duplicate of this question.
Checkout this answer.
You might want to consider saving the files in your app directory of your app, as described in the official pub.dev docs of path_provider.
You can use Dio for downloading and downloads_path_provider_28 for getting download folder path collectively for this:
Future download(String url) async {
final Dio dio = Dio();
Directory? downloadsDirectory = await DownloadsPathProvider.downloadsDirectory; // "/storage/emulated/0/Download"
final savePath = downloadsDirectory?.path;
try {
Response response = await dio.get(
url,
onReceiveProgress: (received, total) {
if (total != -1) {
print((received / total * 100).toStringAsFixed(0) + "%");
}
},
options: Options(
responseType: ResponseType.bytes,
followRedirects: false,
validateStatus: (status) {
return status < 500;
}
),
);
print(response.headers);
File file = File(savePath);
var raf = file.openSync(mode: FileMode.write);
// response.data is List<int> type
raf.writeFromSync(response.data);
await raf.close();
} catch (e) {
print(e);
}
}
I have a flutter application that select and upload audio file to server with asp.net rest api.
my flutter code as follows
uploadFile() async {
print(file.path);
var postUri = Uri.parse("http://192.168.1.100:5041/api/fileup/up");
var request = new http.MultipartRequest("POST", postUri);
request.fields['user'] = 'blah';
request.files.add(new http.MultipartFile.fromBytes('file', await File.fromUri(Uri.parse(file.path)).readAsBytes(),contentType: MediaType('audio','mp3')
));
request.send().then((response) {
if (response.statusCode == 200) {
print("Uploaded!");
}else{
print(response.reasonPhrase);
}
});
}
my file.path value for above flutter code is "/data/user/0/com.mydomain.myappname/cache/file_picker/Over_the_Horizon.mp3" which is returned from file picker.
I am able to upload file with postman, but flutter code gives me 500: Internal Server Error
Postman Screenshot
tried with several codes found on stack overflow , all gave me same error
Did you tried with DIO?
You can accomplish this with DIO, like this:
FormData formData = new FormData.fromMap({
"file": await MultipartFile.fromFile(file.path,
filename: file.path.split('/').last)
});
await dio.post('http://192.168.1.100:5041/api/fileup/up', data: formData, onSendProgress: (int begin, int end) {
var initial = begin;
var done = end;
print('$initial $end');
});
This question already has an answer here:
Upload image with http.post and registration form in Flutter?
(1 answer)
Closed 3 years ago.
I am new to Flutter development. My problem is that I try to upload the image but I keep getting failed request.
This piece of code is where I connect it with a server API which will receive the image file from Flutter. String attachment which consist of the image path that is passed from createIncident function located at another page.
Future<IncidentCreateResponse> createIncident( String requesterName, String requesterEmail,
String requesterMobile, String attachment, String title,
String tags, String body, String teamId,
String address ) async {
IncidentCreateResponse incidentCreateResponse;
var url = GlobalConfig.API_BASE_HANDESK + GlobalConfig.API_INCIDENT_CREATE_TICKETS;
var token = Auth().loginSession.accessToken;
var postBody = new Map<String, dynamic>();
postBody["requester_name"] = requesterName;
postBody["requester_email"] = requesterEmail;
postBody["requester_mobile_no"] = requesterMobile;
postBody["attachment"] = attachment;
postBody["title"] = title;
postBody["tags"] = tags;
postBody["body"] = body;
postBody["teamId"] = teamId;
postBody["address"] = address;
// Await the http get response, then decode the json-formatted responce.
var response = await http.post(
url,
body: postBody,
headers: {
'X-APP-ID': GlobalConfig.APP_ID,
"Accept": "application/json; charset=UTF-8",
// "Content-Type": "application/x-www-form-urlencoded",
HttpHeaders.authorizationHeader: 'Bearer $token',
'token': GlobalConfig.API_INCIDENT_REPORT_TOKEN
}
);
if ((response.statusCode == 200) || (response.statusCode == 201)) {
print(response.body);
var data = json.decode(response.body);
incidentCreateResponse = IncidentCreateResponse.fromJson(data['data']);
} else {
print("createIncident failed with status: ${response.statusCode}.");
incidentCreateResponse = null;
}
return incidentCreateResponse;
}
This is the code snippet where I get the image path from the selected image from the gallery
Future getImageFromGallery(BuildContext context) async {
var picture = await ImagePicker.pickImage(source: ImageSource.gallery);
setState((){
_imageFile = picture;
attachment = basename(_imageFile.path);
});
Navigator.of(context).pop();
}
This is the code where I passed the attachment string to the HTTP Response
this.incidentService.createIncident(
Auth().loginSession.name,
Auth().loginSession.email,
Auth().loginSession.mobile_no,
this.attachment,
this._titleController.text,
this._tags,
this._contentController.text,
this._teamId,
this._addressController.text
).then((IncidentCreateResponse res) {
if (res != null) {
print('Ticket Id: ' + res.id);
// Navigator.pop(context);
this._successSubmittionDialog(context);
} else {
this._errorSubmittionDialog(context);
}
}
You can upload image using multipart or base64 Encode.
For uploading image using multipart Visit the Official documentation
For uploading image using base64 Encode you can checkout the Tutorial Here
I suggest using multipart image upload as it is even reliable when your image or files are larger in size.
Hope this could help you,
create a function to upload your image after picking or clicking an image like,
Future<ResponseModel> uploadPhoto(
String _token,
File _image,
String _path,
) async {
Dio dio = new Dio();
FormData _formdata = new FormData();
_formdata.add("photo", new UploadFileInfo(_image, _path));
final response = await dio.post(
baseUrl + '/image/upload',
data: _formdata,
options: Options(
method: 'POST',
headers: {
authTokenHeader: _token,
},
responseType: ResponseType.json,
),
);
if (response.statusCode == 200 || response.statusCode == 500) {
return ResponseModel.fromJson(json.decode(response.toString()));
} else {
throw Exception('Failed to upload!');
}
}
then you can use use uploadImage,
uploadImage(_token, _image,_image.uri.toFilePath()).then((ResponseModel response) {
//do something with the response
});
I have used Dio for the task, you can find more detail about dio here
Add this to your package's pubspec.yaml file:
dependencies:
dio: ^3.0.5
Then import it in your Dart code, you can use:
import 'package:dio/dio.dart';
To upload image using multipart API use this code ie
Add this library dio in your project in pubspec.yaml file
dio: ^3.0.5
and import this in your class
import 'package:dio/dio.dart';
Declare this variable in your class like State<CustomClass>
static var uri = "BASE_URL_HERE";
static BaseOptions options = BaseOptions(
baseUrl: uri,
responseType: ResponseType.plain,
connectTimeout: 30000,
receiveTimeout: 30000,
validateStatus: (code) {
if (code >= 200) {
return true;
}
});
static Dio dio = Dio(options);
then use this method to upload file
Future<dynamic> _uploadFile() async {
try {
Options options = Options(
//contentType: ContentType.parse('application/json'), // only for json type api
);
var directory = await getExternalStorageDirectory(); // directory path
final path = await directory.path; // path of the directory
Response response = await dio.post('/update_profile',
data: FormData.from({
"param_key": "value",
"param2_key": "value",
"param3_key": "value",
"profile_pic_param_key": UploadFileInfo(File("$path/pic.jpg"), "pic.jpg"),
}),
options: options);
setState(() {
isLoading = false;
});
if (response.statusCode == 200 || response.statusCode == 201) {
var responseJson = json.decode(response.data);
return responseJson;
} else if (response.statusCode == 401) {
print(' response code 401');
throw Exception("Incorrect Email/Password");
} else
throw Exception('Authentication Error');
} on DioError catch (exception) {
if (exception == null ||
exception.toString().contains('SocketException')) {
throw Exception("Network Error");
} else if (exception.type == DioErrorType.RECEIVE_TIMEOUT ||
exception.type == DioErrorType.CONNECT_TIMEOUT) {
throw Exception(
"Could'nt connect, please ensure you have a stable network.");
} else {
return null;
}
}
}