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

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

Related

Flutter Dio multipart request image save name

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

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

Soap message encoded in base64 binary data multiple files

Recently I got a new project to work on a SOAP service and to Get and Post messages to a ASP.NET service based on xml.
The issue is that I managed to make the soap request and get the message.
The message looks like this:
UEsDBBQAAAAIAAdUe06+NXE0kR4AADLSAQALAAAAUHJvZHVzZS54bWzUXW1z48YN5k/h5EMnmbMsvomSpmkzFCXbjERJoSTb52/p9dq5mbxN2svczy/........
The message is Base64 Binary on RFC 4648 with multiple xml documents on it.
How I can construct this documents from the code in php?
The documents encrypted in this request are 3 xml files.
I managed to get them from an online decryptor called freeformatter with download function.
If I try to decode the result I get something like:
PKT{N�5q4�2�Produse.xml�]ms��
�O��C'��,����i3%یDI�$��o��ڹ��M����/�,��|vL�O�$�/�xv,,�u�s>9?;?....
Is there a solution for this?
I'm new to SOAP so I don't understand too much of it.
Thank you but i mannged to solve it.
I gonna post here the sollution so everyone who facing the same issue, get the response.
The first thing you need to do when you have an .zip file in a base64 binary string is to catch the response to a txt file.
Let's say the response from soap it's called ' $response ' and we need to catch this to an file. We do like this :
$response = $client -> _getLastResponse();
$fille = "response.xml";
fille_put_contents($fille,$response);
Now we got the response to an xml file.
The next thing to do is to get the response from xml values.
Lets say our value is <ResponseFromServer> .
`$b64 = "b64.txt";
$dom = new DomDocument();
$dom = load("response.xml");
$data = $dom->getElementByTagName("ResponseFromServer");
$catchb64 = $data;
fille_put_content($b64,$catchb64);`
Now we got the clean Base64 Binary string in one fille.
The next thing we need is to create the document ( in this case is a .zip fille)
`$input_fille = "response.txt"; // the fille with clean base64 binary data on it
$output_fille = "result.zip"; //the fille we need to create on system with the
documents decrypted
$content = fille_get_contents($input_fille); // Reading the content of input fille
$binary = base64_decode($content); // Decoding to binary
fille_put_contents($output_fille,$binary); // Writing to fille the vallues`
We dont need the ZipArchive() function, because is allready a zip archive, all we need to do is to create a empty document and after to send the binary data to it.
Cheer's and goodluck!

NetSuite RESTlet output pdf

NetSuite Restlet PDF file encoding issue
The above thread seems to be giving a solution to outputing a pdf with a NetSuite RESTlet. As far as I know, you cannot output a pdf from a restlet, so I'm very confused. I am using a restlet to generate a report and the information ultimately needs to output to a pdf so I was trying to see if there was a work around. I tried the answer code from the above thread and I got the expected error:"error code: INVALID_RETURN_DATA_FORMAT error message:Invalid data format. You should return TEXT."
Am I missing something? Is there a way to export xml to a pdf with a NetSuite RESTlet?
The thread you reference discusses how to generate a PDF file in Netsuite. If you want to return a PDF from a RESTLet you will have to return it as a member of a JSON object. e.g.:
var pdfFile = genPDF(); // base this on the sample
return{
fileName: pdfFile.getName(),
fileContent: nlapiEncrypt(pdfFile.getValue(), 'base64')
};
And then your receiver will have to create the actual file.
Recall that RESTLets are for application-to-system communications. If you are trying to return a PDF to a browser you should probably be using a Suitelet.
If this is part of a larger app and you need the RESTLet then review this post: Save base64 string as PDF at client side with JavaScript for options to display the RESTLet response.
Reading through that answer, it appears you'll need to encode/convert the PDF to string format before returning, so you'll need to use base64 encoding.
The NS method nlapiEncrypt(content, 'base64') seems like it might be a good place to start.
Another avenue to investigate, which I haven't tried, is to first save the PDF in the file cabinet, then to return a public link to that file. You'll need to make sure the file has the correct permissions.

Get the name of an uploaded file

I'm uploading a file using curl:
curl -X POST --data-binary #/home/me/my_file.jpb localhost:9001/upload
And here is how to store it:
def upload = Action(parse.temporaryFile) {
request =>
import java.io.File
val f = new File("tmp/someName") // how do I get the name of the file being uploaded?
request.body.moveTo(f, true)
Ok("File uploaded\n")
}
Note that files can be in any format. I want to get the name of the actually uploaded file. I tried request.body.file.getName but it returns gibberish.
How do I do that?
I am fairly certain you cannot get the file name from the binary stream you are uploading via curl. You need to explicitly provide the file name separately.
The options I can think of are these:
If your Content-Type header is instead multipart/form-data, then the process is quite simple as described here
Upload JSON with a String for the file name and a binary portion for the file.