I'm trying to convert base64 strings back to the original files. The application from where I try to export these files will only allow me to export in base64 strings. This export returns the base64 string and the filetype.
How can I convert these strings back to the original files? I've been trying things like this, but I don't think this will work with different types of files?
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($file)) |
Out-File C:\ID\document.$($extension)
Can anyone provide me some ideas on how to do this?
The FromBase64String() method converts a base64-encoded string to a byte array. All you need to do is write that byte array back to a file:
$b64 = 'AAAAAA...'
$filename = 'C:\path\to\file'
$bytes = [Convert]::FromBase64String($b64)
[IO.File]::WriteAllBytes($filename, $bytes)
Related
I am trying to find an example, or documentation, on creating a picture (floating shape object) inside the Excel sheet. The source is supposed to be a numeric bitmap data, stored in a VBA array acquired using external I/O libraries. Using Excel cells as an intermediary storage is possible, but not desired, since the RGB bitmap data is expected to be huge.
The task itself seems to be extremely simple in matlab-like environments, or python. But I just have no Idea how to make it in Excel and VBA without importing an independent image file from the file system.
In terms of storing the file, how huge is 'huge'? If you convert the image into Base64, it'll be a fairly trivial task to split it up amongst the cells and then reconstitute it when converting it into an image.
Alternatively, you can store the Base64 string in a standard module - I'm currently doing much the same thing, but my image only clocks in at 100kb (better to save it as a PNG rather than BMP).
In terms of converting the Base64 string to an image, the Windows Image Acquisition COM object will convert a byte array into a stdPicture image type (and further to my point above, it will also accept PNG files...]. The following function accepts a Base64 string, converts it into a byte array, and returns an stdPicture object:
Function Base64toStdPicture(ByVal Base64Code As String) As StdPicture
Dim ImgVector As Object
Dim Node As Object
Set ImgVector = CreateObject("WIA.Vector")
Set Node = CreateObject("Msxml2.DOMDocument.3.0").createElement("base64")
Node.DataType = "bin.base64"
Node.Text = Base64Code
ImgVector.BinaryData = Node.nodeTypedValue
Set Base64toStdPicture = ImgVector.BinaryData.Picture
Set Node = Nothing
Set ImgVector = Nothing
End Function
From that point, you can out it in an image control, or copy it to / from the clipboard, etc.
I have been trying to use the inbuilt dataUtils.downloadFile function from JHipster on the angular side. This accepts a content string and content type and let the user download the content as a file.
I noticed that, it can easily process content which contains ASCII character. However, it fails to process UTF-8 character set.
This is the error I get :
Failed to execute 'atob' on 'Window'
Am I missing something or is there a way to get around with it?
Currently i have to go through my file to replace all UTF-8 only chars to ASCII but that would be tedious.
Thanks for reading..
EDIT:
Below is the field definition.
{
"fieldName": "troubleshooting",
"fieldType": "byte[]",
"fieldTypeBlobContent": "text"
}
Here is the angular code which tries to convert the string to base64 and then download.
The problem is not with base 64 encoding. It is fine. The problem is with content format. If the content contains UTF-8 only chars, then it fails. In other cases I get the file downloaded successfully
download(appliance: Appliance) {
const applianceObj = JSON.parse(appliance.troubleShooting);
const prettyPrinted = JSON.stringify(applianceObj, null, 2);
const data = this.base64Utils.encode(prettyPrinted);
this.dataUtils.downloadFile('application/json', data, appliance.applianceType);
}
How to find the file type whether it is an image or ppt or pdf from Base64 string.
How can i decode that string and also get the file extension of Base64 String.
It should be in AngularJs.
If the string was obtained by FileReader.readAsDataURL() (see https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL) it should be similar to this one:
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQg ... "
So the first part is the MIME_TYPE (data:image/jpeg;), then there's a delimitator (base64,) and finally the base64 string which can be decoded using a library like this one:
https://github.com/ninjatronic/angular-base64
when using the ETCD dashboard, we find a binary string like
var _deps_js = "\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xcc\xbd\x7b\x77\xdb\x46\xb2\x2f\xfa\xff\xf9\x14\x22\xc6\x47\x01\xcc\x16\x25\xd9\x99\xb9\x7b\xc0\x40\xbc\x89\x1d\x27\xce\x24\x76\x26\x76\x26\x0f\x8a\xc9\xc2\x8b\x24\x24\x92\xa0\x49\xca\x92\x23\x72\x3e\xfb\xad\x5f\x55\x77\xa3\x01\x82\x72\xf6\xec\x7b\xd6\x3a\x99\xb1\x88\x47\xa3\x9f\xd5\xd5\xf5\xae\xce\xf8\x66\x91\x6e\x8a\x72\xe1\xc7\x2a\x09\xee\xbd\x32\xb9\xca\xd3\x8d\x17\x45\x9b\x0f\xcb\xbc\x1c\x1f\xcd\xcb\xec\x66\x96\x1f\x1f\x1f\x78\xd1\xcb\xef\x96\xe5\x6a\xb3\x1e\xd4\x6f\xa3\xb8\x97\x95\xe9\xcd\x3c\x5f\x6c\x06\x09\xd5\xdc\x39\x0b\xc2\xaa\xa1\xe0\xbe\x18\xfb\x9d\xaa\x48\xb0\x99\xae\xca\xdb\xa3\x45\x7e\x7b\xf4\xe5\x6a\x55\xae\x7c\xef\xea\x9f\x37\xf9\xea\xc3\xd1\x2a\x7f\x77\x53\xac\xf2\xf5\x51\x7c\x74\x5b\x2c\x32\x2a\x73\x5b\x6c\xa6\x74\x67\xbe\xf4\x82\xfe\x2a\xdf\xdc\xac\x16\x47\xd4\x4a\xb0\x0b\xf9\xaf\xef\xdd\x2c\xb2\x7c\x5c\x2c\xf2\xcc\xeb\x98\xee\xca\xf7\x03\xf9\x09\x37\xd3\x62\xad\xea\x23\x37\x77\x47\x29\x3a\xf8\x3e\x5e\x1d\x25\x34\x8a\x59\xbe\x98\x6c\xa6\x2a\x8d\xe2\xa4\x87\x9a\xe8\x9d\x6e\xd1\x33\x1f\xd0\x94\x44\xe9\x76\x4b\x05\x8a\xf5\x4f\x5c\x3d\x15\x1a\x74\xce\xc3\x73\x7a\x11\xf7\x16\x65\x96\xbf\xa5\x2f\x8f\x8f\x93\x41\xe7\x2c\xf4\xe2\xd5\x2a\xfe\xa0\xbf\x39\xa3\x9f\x64\xbb\xf5\x16\x37\xf3\x24\x5f\x55\x73\x9b\x50\xe1\x8b\x33\xfa\x73\x72\x7e\x54\x2c\x8e\xe2\x9d\xed\x5c\x86\xce\xaa\x94\x67\x90\x5b\x7c\x61\x06\x91\x04\x81\x9e\x0a\x7a\x3e\x59\xe5\x4b\x2a\xe
it's a long list.
Is there a tool to convert this into a readable text so that I can know the exact the content.
The two leading bytes "\x1f\x8b" indicate the data is gzip compressed.
You can use whatever gzip/gunzip library/utility you have to decode it.
Following is an example using Perl.
use IO::Uncompress::Gunzip;
my $in = "\x1f\x8b..."; # <- your string goes here
&IO::Uncompress::Gunzip::gunzip( \$in => \my $out );
print $out;
and it returns something like
!function(a,b){"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}("undefined"!=typeof window?window:this,function(a,b){function c(a){var b=a.length,c=ab.type(a);return"function"===c||ab.isWindow(a)?!1:1===a.nodeType&&b?!0:"array"===c||0===b||"number"==typeof b&&b>0&&b-1 in a}function d(a,b,c){if(ab.isFunction(b))return ab.grep
Since the string in your question is truncated, above output is truncated.
Recenlty i've found that not all text (.txt) files could be readed as i need in adobe air. Because of diff file encodings (unicode, utf-8, ascii).
For example:
var fDataStream:FileStream;
var textfile:File = new File ("C:\myfile.txt");
var sContent:String;
fDataStream = new FileStream();
fDataStream.open(textfile,FileMode.READ);
sContent = fDataStream.readUTFBytes(fDataStream.bytesAvailable);
fDataStream.close ();
If 'myfile.txt' is not utf-8 encoded, then i get string like that "ÿþE"
I know that there is fDataStream.readMultyBytes() method, but it requries string representing file charset that can't be known beforehand (input .txt files for app could be in diff charsets). I'am out of ideas.
Thanks.
I think you want to use .readbytes instead of .readUTFBytes
That should load anything you give it.
see
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html#readBytes()