I have a pdf with encoding UniJIS-UCS2-H. i want to convert it in to base 64 in javascript.
i wrote the code like this. It converting it in to pdf but the content is not visible.
my code is showing below.
function getBinary(file){
var xhr = new XMLHttpRequest();
xhr.open("GET", file, false);
xhr.overrideMimeType("application/pdf; charset=UniJIS-UCS2-H");
xhr.send(null);
return xhr.responseText;
}
function base64encode(binary) {
return btoa(unescape(encodeURIComponent(binary)));
}
var binary = getBinary('http://localhost/first.pdf');
var base64encoded = base64encode(binary);
data = "data:application/pdf;base64,"+base64encoded;
the data is contain only a plain pdf. how can i resolve it ?
Related
I am trying to input a file and get the same output as php's file_get_contents() but doing so in javascript. My javascript code is below:
var xhr = new XMLHttpRequest();
xhr.open( "GET", "http://localhost:3000/line.png", true );
xhr.responseType = "arraybuffer";
xhr.onload = function( e ) {
var arrayBufferView = new Uint8Array( this.response );
console.log(arrayBufferView);
var blob = new Blob( [ arrayBufferView ], { type: "image/png" } );
var reader = new FileReader();
reader.onloadend = function () {
var blobStr = reader.result;
console.log(blobStr);
}
reader.readAsText(blob, "iso-8859-1");
};
xhr.send();
When I use the above code I get what appears to be the exact same output as php's file_get_contents(). However it ends up being different output because when I md5 each string, I get different results. My example image is below:
Looking at the javascript string outputted in a div, I see this:
Looking at the php string outputted in a div, I see this:
As you can see, both look the same, except for maybe some formatting differences? Would this be the cause of my issues? How would I force the javascript version to have the same output as the php version?
I need to download an image file when user click a download button.
I have the content of the file in utf-8 format returned from the server.
ex: "�PNG IHDR#�v/j�sRGB���gAMA���a pHYs���o�d��IDATx^�� �TՕ�_�~��^>�~��N�M�:/��t�Iw�I^��̜8���I#шH��DC�(*" ..... "
( The exact file string displayed in http://codepen.io/jduprey/details/xbale when I upload the same file in this web site)
Now I need to create the blob of the file and save it in the client side.
I tried FileSaver( https://github.com/eligrey/FileSaver.js) library as follows
var blob = new Blob( [ utfFileString ], { type: 'image/png' });
saveAs(blob, aData.name );
But downloaded files are in incorrect format and and cannot be opened.
Appreciate if someone can help me on this.
Thanks!
Png files are not utf-8 encoded, notice all those invalid code sequence replacement characters(�) in the string.
You need to get the data in binary format, such as blob or array buffer. Something like
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200){
...
var blob = this.response; //The binary data
...
saveAs(blob, aData.name);
...
}
}
xhr.open('GET', 'path/to/file');
xhr.responseType = 'blob'; // the response will be a blob and not text
xhr.send();
I'm trying to render vtk objects which are send from a webserver directly on the client using XTK without storing them to the disk. According to the XTK Documentation I just have to pass the vtk file as string into X.Mesh.filedata, but it just doesn't display anything when I'm trying to do that.
I want to do something like this:
var data = recieveVTKFileAsStringFromServer();
var r = new X.renderer3D();
r.init();
// create a mesh from a .vtk file
var dataset = new X.mesh();
// dataset.file = 'someFile.vtk';
dataset.filedata = data;
// add the object
r.add(dataset);
// .. and render it
r.render();
When I load the file from the file everything works just fine, setting it using filedata doesn't. Where is my mistake?
I also came up with the similar scenario to load the binary data directly using filedata instead of setting file attribute. I did this by passing dummy name in a file attribute along with actual binary data set in filedata and everything works fine.
var xhr = new XMLHttpRequest();
xhr.open('GET', '/test.nii', true);
xhr.responseType = 'arraybuffer';
xhr.send();
xhr.onreadystatechange = function (e) {
if (this.readyState === 4) {
var r = new X.renderer2D();
r.container = 'myImg';
r.orientation = 'Z';
r.init();
volume = new X.volume();
volume.file = "abc.nii";
volume.filedata = this.response;
r.add(volume);
r.render();
}
};
i have a medium blob data type on my database, it is storing pictures with gif,jpg,png,bmp types.
i am retrieving the blob using jsf.
i have read this question -> Using Javascript to Display Blob
but i was confused how he converted his blob to base64.
this is what i have done so far.
var blob;
var base64;
var image;
<ui:repeat value="#{testController.allItems}" var="item">
blob = "#{item.logo}";
base64 = btoa(blob);
image = document.createElement('img');
image.src = 'data:image/png;base64,'+ base64 ;
$('#testDiv').append(image);
</ui:repeat>
the above code just gives me a broken link pictures.
sample blob from inspect element
blob = "[B#2d8f2913";
and the image inside the div
<img src="data:image/png;base64,W0JAMmQ4ZjI5MTM=">
UPDATED my code based on Ian's suggestion
i created a method RenderLogo
public String RenderLogo(byte[] rawLogo) {
String base64String = Base64.encodeBase64URLSafeString(rawLogo);
return base64String;
}
now on my js:
var base64;
var image;
<ui:repeat value="#{testController.allItems}" var="item">
base64= '#{testController.RenderLogo(item.logo)}';
image = document.createElement('img');
image.src = 'data:image/png;base64,'+ base64;
$('#testDiv').append(image);
</ui:repeat>
it gives me a console error in inspect element
GET data:image/png;base64,_9j_4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP_sABFEdWNreQABA…CAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgP__Z
this is whats written in the js when i inspect element
blob = '_9j_4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP_sABFEdWNreQABAAQAAAA8AAD_7gAOQWRvYmUAZMAAAAAB_9sAhAAGBAQEBQQGBQUGCQYFBgkLCAYGCAsMCgoLCgoMEAwMDAwMDBAMDg8QDw4MExMUFBMTHBsbGxwfHx8fHx8fHx8fAQcHBw0MDRgQEBgaFREVGh8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx__wAARCADLAMgDAREAAhEBAxEB_8QAsgABAAIDAQEBAAAAAAAAAAAAAAUGAwQHAQIIAQEAAgMBAQAAAAAAAAAAAAAAAwQCBQYBBxAAAQQBAQQFBQsHDAMAAAAAAQACAwQRBSExEgZBUWEiE3GBkTIHobHB0UJSYnIjFBWCkqKyM3M28MLSQ5OzJDRUdDUW4eIXEQACAQMABwQJAwUAAwAAAAAAAQIRAwQhMUFREgUGYXGBwfCRobHR4SIyE2IUNPFCUjMWcoIV_9oADAMBAAIRAxEAPwD9UoAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAxy2K8X7WVkf1nBvvr1I8cktZi_FNN_1cP8AaM-Ne8L3GP5I70Zop4JRmKRsg-iQfeXjRkpJ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4RSEd5oeMOAPbjagNWPQtFjv_AIhHRgZeLnPNlsbRJxPHC48QGckHagNLU-UtKu6lU1FsbK9ytZZZkmjY0PlMYIDXu34QErHSqR2pbccLG2Zw1s0wADnhmxocenCA1maBojLxvsoQNul5kNlsbRIXkEF3EBnJyUBsmlTMc0RgjMVgl07C0Fry4YcXA7DkDpQHz-HUPun3M12Grv8ABLQW5zxZweni2oD1tGk2COBsDGwROD4ow0BrXNdxAgDpDtqA9jpVI3tfHCxr2GRzHAAEGZ3HIR9d20oDMgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgP__Z';
image = document.createElement('img');
image.src = 'data:image/png;base64,'+ blob ;
UPDATE 2
ahh! fixed it! now its working.
changed Base64.encodeBase64URLSafeString(rawLogo); to Base64.encodeBase64String(rawLogo);
I wouldn't be surprised if the problem is that the way the JSP renders item.logo on the page is messing it up. Since you have no use for the byte array in JavaScript, you can convert it in Java before printing it on the page. Also, this relieves the need for window.btoa, which isn't natively supported in IE before version 10.
Try creating a method in your class that returns the base64 encoded value of the logo property, called something like getLogo64...then in your JSP, you can reference it like: base64 = "#{item.logo64}"; and not even deal with the logo property. So an example is:
public String getLogo64() {
return Base64.encodeBase64String(this.logo);
}
At the same time, you can add a basic method that accepts a byte[] and converts it (which is what you have found to work):
public String RenderLogo(byte[] rawLogo) {
String base64String = Base64.encodeBase64String(rawLogo);
return base64String;
}
and use it like #{testController.RenderLogo(item.logo)}.
Reference:
encodeBase64String: http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html#encodeBase64String(byte%5B%5D)
If the resource you're loading has the appropriate protocol and CORS headers available, you could load it as a Blob, then add it to your document like this using the URL.createObjectURL(data) method to get the img src:
function urlToDataBlob(file_url, callback){
var xhr = new XMLHttpRequest();
xhr.open('GET', file_url, true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
console.log('urlToDataBlob: load: ', e);
if (this.status == 200) {
callback(this.response);
}
};
xhr.send();
}
// eg:
urlToDataBlob('https://the.url/to.some.jpg', function(data){
console.log('data: ', data);
var _img = '<img src="'+ URL.createObjectURL(data) +'">';
// add to your document..
});
See : https://www.html5rocks.com/en/tutorials/file/xhr2/
Is it possible to view the raw data for an image file in javascript?
I'm trying to write a script that converts an image into its hex dump.
How can I view the data I'm writing to the image file?
You can do this with XHR:
var xhr = new XMLHttpRequest();
xhr.open('GET', '/my/image/file.png', true);
xhr.responseType = 'arraybuffer'; // this will accept the response as an ArrayBuffer
xhr.onload = function(buffer) {
var words = new Uint32Array(buffer),
hex = '';
for (var i = 0; i < words.length; i++) {
hex += words.get(i).toString(16); // this will convert it to a 4byte hex string
}
console.log(hex);
};
xhr.send();
Look at the doc for ArrayBuffers and TypedArrays
And you can see how I use it in testing here
Is it possible to get the source code of a file in javascript?
Find the URL for the script and load that into your browser. (Or use Firebug)