Grails - Loading image from the controller - image

I am trying to retrieve zip file from FTP, unzip it, and get xml file and image file from them and parse the xml, display the contents of xml and image.
byte[] image = ftpClientService.getThumbnailInZip(customer.ftpUser,
customer.ftpPassword, customer.ftpHost, customer.ftpToWrapDirectory,
fileName)
FileOutputStream fos1 = new FileOutputStream("zip.img")
try {
fos1.write(image);
} finally {
fos1.close();
}
return [
command: this,
fileName: fileName,
applicationName: applicationName,
contentProvider: contentProvider,
operatingSystem: operatingSystem,
handSets: handSets,
zipImg:"zip.img" ]
I could finish the xml part successfully and image also I am able to retrieve from the zip in a byte format( i could convert it to a file using file outputstream),
Now I am stuck in sending the image to gsp and to display that. Any inputs are much appreciated.
Thanks

If you want to use the image only once, meaning it should always be extract from the zip file, then embedding the img in base64 format into the webpage is a good option here because you don't need to worry about the image file after sending that base64 encoding value to gsp.
If you still need that image file to be used by other http requests then you should extract the images to a folder and send the list of img paths to gsp.

You can either
point img src="${g.createLink(action: 'a', params: [p: p])}" to some other action (with createLink) that will cache the image on your server side,
or embed it right into HTML, like in this question.

Browsers can render byte arrays if you specify the format.
Having a variable image in the model sent to the gsp of type byte[], this is the way to render it HTML:
<img src="data:image/png;base64,${image.encodeBase64()}"/>
You also need to specify if it's image/png or other format.

Related

How to save the image URL at the exact position where the image was included in React-Quill (MERN stack)?

I am building an application, on the front end there is there are a few fields, including a rich text editor (react-quill) that can take in an image. I have saved the values from the first page and displayed them back to the user for confirmation. In order to do so I used local storage in the form of formData as shown below
formData = {"questionTitle": "Here is a test field",
"questDesc": "<p>Here is the Image <img src=\"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU.. />” }
My problem is after confirmation, I am trying to push the formData to MongoDB but I need to save the image (in cloudinary) separately from the database. How do I send the image to Cloudinary and instead save the URL in the MongoDB at the exact position that was inserted in react-quill?
I want to know if there is a better solution to my problem. Currently, the image is in base64 in local storage, is there a way to just refer to the route of the image while in local storage, and push it to the cloudinary after confirming? All pointers, and suggestions welcome!!

Attach a filename to a base64 image in a JSON file

I have an application that gets a JSON file via a web service. The JSON is fairly large, and represents a Person object, with typical properties such as first name, last name, title and image for example. The person's image is stored as a base64 field in the JSON. It is bound to an image tag using Angular. So for example, the HTML looks like this:
<img ng-src="data:image/jpeg;base64,{{ person.fileImage }}">
When the end user right clicks on the image and chooses to save the image, the browser defaults to the name "download.jpg". What I need to do is name the image so that when the user right clicks and chooses to save, it gives a meaningful filename, e.g.:
todd.davis.jpg
I'm not sure how to make this happen. I've seen some solutions that use an anchor tag with a download="todd.jpg" parameter, but that is not working for me. I think it expects an actual URL and in this case, I don't really have one. The image data is just embedded in the JSON.
Is there a way to manipulate this so that I can add a name to the image for saving purposes?

Fine Uploader, how can I manage scaled images names and extensions on client side?

I need to upload only scaled images on the server.
For original + scaled images docs say:
The code above will result in Fine Uploader generating and uploading 2 scaled versions of each user-submitted image file. The original, along with the 2 scaled versions, will appear in the UI's file list as well. If the user submits a PNG named "image.png", that will be submitted, along with another file named "image (small).png", and "image (medium).png".
I guess it's the same for only scaled version, but the file I'm receiving on server side is just named blob without any extension!
I'm using this script
var uploader = new qq.FineUploader({
...
scaling: {
sendOriginal: false,
sizes: [
{name: "scaled", maxSize: 1280}
]
}
});
How can I set at least the original file extension (and maybe the name aswell) to the blob file from the client side?
Thanks
From the "Notices" section on that same doc page:
The scaled file names will be sent with the upload request as the qqfilename parameter. Be sure to read this parameter when naming your file server-side.

Insert image in wkhtml to pdf

Hi I am using wkhtml to pdf method to genearte a pdf , I need to insert a image in my Pdf Header but i dont want to show the PDF header Image in my HTML page. How can i take direct image for my PDF Header.
Thanks in advance.
probably it could be reached via this:
$pdf->addPage('<html><div id="header">AHOJ</div></html>');
$pdf->addPage('/path/to/your.html');

Selenium copy image to Base64 string?

How can I get an image on a webpage with Selenium and encode it to a Base64 string which gets added to a variable? I'm using Selenium C# but any language will probably work.
I am not quite sure what you are asking. What do you mean by "get an image on a webpage"? Do you mean:
grab a screenshot of your page and compare it with some given value? or
take a screenshot of specific element on webpage?
download an image contained in (ie) <img> tag and do something with it?
For taking screenshots, it is widely disucessed here. Although mostly java solutions, they probably could be ported to C# with ease. If what you need is nr 3, then get the URL (ie using xpath //img[#id=\"yourId\"]#src ) and download it using something like WebClient and convert that to base64:
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
var baseString = System.Convert.ToBase64String(plainTextBytes);
This code will helps you, I am using it for my own report, instead of storing report in seperate location, better to convert into base64 form and add it to report.
String Base64StringofScreenshot="";
File src = ((TakesScreenshot) driverThread).getScreenshotAs(OutputType.FILE);
byte[] fileContent = FileUtils.readFileToByteArray(src);
Base64StringofScreenshot = "data:image/png;base64,"+Base64.getEncoder().encodeToString(fileContent);

Resources