Flutter Dio multipart request image save name - flutter

I'm using Dio for API calls. my question is I have chosen an image from the file and named as "path+flower.jpg". So in the Multipart request, we have one parameter Filename. While saving the image to S3 what will the image name flower.png or flower_one.jpg?
MultipartFile.fromFile("path+flower.png", filename: "flower_one.jpg"),
Thanks.

Dio multipart request has:
filePath in which you can give path of image (it is required parameter)
filename is optional parameter
if you provide filename image will be saved with that filname
in your case image will be saved as:
flower_one.jpg

Related

How to write a File variable in Dart without saving it?

I am following the pdf Dart Package where it shows how to save a PDF file:
// On Flutter, use the [path_provider](https://pub.dev/packages/path_provider) library:
// final output = await getTemporaryDirectory();
// final file = File("${output.path}/example.pdf");
final file = File("example.pdf");
await file.writeAsBytes(await pdf.save());
What I am looking to do is send a PDF through Sendgrid with uses Base64 encoding as the attachment. This is what I have done but with an error:
Uint8List unitEncodedPDF = await pdf.save();
String baseEncodedPDF = base64Encode(unitEncodedPDF);
I then use baseEncodedPDF and pass it to the content key in the body for the Sendgrid POST.
What I think I need to fix my "The attachment content must be base64 encoded." error message:
Save the PDF file as a File object in Dart like in the pdf package tutorial, and then convert the 'file' to Uint8List and then finally Base64.
My problem:
File.writeAsBytes saves an actual file to the phone's storage, the information is sensitive and I don't want it saved on the user's phone but send through the API. How do I save the PDF file as a File object to use it as a variable to Base64 encode it, without writing an actual file onto the storage?
-why dont you create a global variable in class and set your data into it and send it through api
-or delete file after it sent to API with encrypted name and file extension this would make it hard to user to locate manually

Unable to Load image from url in flutter

I am Fetching images from URL and it is providing broken images while on the web the same URL is providing complete and accurate images.
Scheme not starting with alphabetic character
throwing error.
// Depending on where the exception was thrown, the image cache may not
// have had a chance to track the key in the cache at all.
// Schedule a microtask to give the cache a chance to add the key.
Image Url:- https://smott.world/upload/source/1646761765-WhatsApp Image 2022-03-08 at 5.41.29 PM.jpeg
This is happening because your url is not encoded, you can encode using Uri.encodeFull to encode. Encode url will replace white spaces by %20
Example:
var uri = Uri.encodeFull('https://smott.world/upload/source/1646761765-WhatsApp Image 2022-03-08 at 5.41.29 PM.jpeg');
var url = uri.toString();
// url will be "https://smott.world/upload/source/1646761765-WhatsApp%20Image%202022-03-08%20at%205.41.29%20PM.jpeg"
You can see more about percent encoding here

Getting pdf data from cloudinary and saving it in flutter

I have been using cloudinary to store my pdf files the use firebase as my backend. However, am having this issue with it. When I use http.get on the link provided by cloudinary https://res.cloudinary.com/cloudname/raw/upload/vxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxx I get ascii data instead of the pdf file. I have tried to write the data as string to a file with .pdf extension that has not worked. I have tried to convert it to bytes then write to the file as bytes and that has not worked either. Any help on how I can save this file on android using flutter It would be a great help.
await http.post('https://api.cloudinary.com/v1_1/${DotEnv().env['cloud_name']}/raw/upload',body: { "file": "data:raw/pdf;base64,$pdf", "upload_preset": DotEnv().env['upload_preset']} );
var response = await http.get('https://res.cloudinary.com/<cloudname>/raw/upload/v1590958359/gsvxe4zp7bb6yyldrccu');
final directory = await getApplicationDocumentsDirectory();
final path2 = directory.path;
File x = File('$path2/trial.pdf');
x.writeAsString(response.body);
Here is the code as I currently have it. pdf in this case is the base64 encoding of the pdf. The link I have here in http.get is from a file I had already uploaded.
Thanks for adding the additional details.
Firstly, I would recommend you change the resource_type from raw to image. In addition, please update the MIME type in your Base64 Data URI to application/pdf.
The URL you then get from Cloudinary upon a successful upload will be to a PDF resource which means you can download it with a simple cURL request or via any HTTP library.
For example, using the secure_url to the asset returned in the Upload API Response to download the file to my_test_file.pdf -
curl -s https://res.cloudinary.com/demo/image/upload/multi_page_pdf.pdf --output my_test_file.pdf

POST image to web service with Flutter

I am currently using Flutter and Dart to try to send an image to a web service, after which I wait for a JSON response. The below links show the Postman request that I am trying to mimic.
An image of Postman with the headers required
An image of Postman with the body
I think the issue I am having is either not setting the headers of my request correctly, or not encoding the image correctly.
Currently I am getting a HTTP 400 error.
I have tried following these suggested solutions on StackOverflow but have been getting the HTTP 400 error.
Any help would be much appreciated!
Try this. I'd suggest creating yourself a plain Dart project, if you haven't already. This way you can test things without the need of the phone emulator, etc.
main() async {
http.MultipartRequest request =
new http.MultipartRequest('POST', Uri.parse(url));
request.headers['Prediction-Key'] = '3f4a......'; // todo - insert real value
request.files.add(
new http.MultipartFile.fromBytes(
'image',
bytes,
filename: 'somefile', // optional
contentType: new MediaType('image', 'jpeg'),
),
);
http.StreamedResponse r = await request.send();
print(r.statusCode);
}
If the file is on disk, not in memory, then use the fromPath named constructor instead. Experiment with different media types. I've used image/jpeg, but you could try application/octet-stream.
As an aside, in your first screenshot you show a content type, but Postman ignores this as the overall content type is overridden by multipart-form. Uncheck that row in Postman to prove this.
There was another question recently on SO, where the server was incorrectly expecting headers to be case sensitive. In postman, try again with lowercase prediction-key to prove that the server doesn't mind lowercase headers (which is what Dart uses).

Upload multipart file as JSON encoded

I'm trying to upload a multipart file as JSON encoded, rather than URL encoded. It seems to default to URL encoded, but I can't figure out a way to change this. With other Alamofire methods you can explicitly state the encoding like:
Alamofire.request(_, _, parameters: _, encoding: .JSON)
Is there a similar way I can change the encoding on the Alamofire upload function?