Download file using angular - other character encoding in db and html - javascript

I saving data in database(sql server) using Latin1. My html application uses utf-8 encoding.
Server side is wrote in c# using WebApi.
When I try to download file from db which contains word ' 123' in txt file:
$http.get<string>(url).success(data=> console.log(data);
I get this: �123
After use this
unescape(encodeURIComponent('�123'));
I get "�123"
How can to convert returned string from server to proper one ?

Related

How to convert a file encoded to base64 string by javascript to a Ruby file object?

I am trying to upload a file to Rails 6 Activestorage api (using ReactJS), the docs for this are not very clear to me but after lots of searching, it seems best to use:
obj.attachment_name.attach(file)
This works well if I have a file on the Rails end, however passing the file from the React to the Rails side is a bit tricky, the best option I found was to transform the file to base64 string & send it to the API
So how to convert a javascript file passed as base64 string on the Rails side to a file object?
I tried this answer, but surprisingly, it doesn't convert the string to a file (I wonder if that is because JS conversion is different than Rails base64 conversion)
Note:
This works well to encode & decode files using ruby only
# Testing plain ruby
# Open the file you wish to encode
file_path = "/Users/...path/some_image.jpg"
data = File.open(file_path).read
# Encode the image
encoded = Base64.encode64(data)
# Why this block doesn't work for a JS encoded base64 string??
# i.e, if I passed encoded string from JS here, it won't work
File.open("some_filename", "wb") do |file|
file.write(Base64.decode64(encoded))
end

NodeJS read file into string without encoding like PHP file_get_contents

I'm trying to process image and pass image data from a NodeJS app to hit an API wrote in PHP.
I use fs.readFileSync to read the image file (I'm using PNG here). The API only takes string as file content to upload. It seems like PHP file_get_contents doesn't have a specified encoding as I tried mb_detect_encoding($fileContent) and outputs false and the fileContents starts like \x89PNG\r.
I'm using Node v8 and it seems like i have to use some encoding to convert Buffer to string. I tried couple of encoding like base64, binary and the fileContent starts like \xc2\x89PNG\r or \xef\xbf\xbdPNG\r.
What is the equivalent of PHP file_get_contents in NodeJS? How can I get the right format of image data?
Thanks!
If I understood you right, you want to read an image file using nodeJS and send the image as a string to an API which is built using PHP. Have you tried first encoding it to base64 and casting it as string ?
var fs = require('fs');
// read binary data
var png= fs.readFileSync(pathToImageFile);
// convert binary data to base64 encoded string
var imageString = new Buffer(png).toString('base64');
Then send the imageString as a string, hope this helps!

Ionic save base64 string to file from PHP API

I'm currently working on a Ionic project. We want to transfer Files using authentication, so I do a request to the server with my session token in the header and then the server returns a piece of JSON code. This JSON contains the file type (.png, .docx, etc) and a base64 string of the file.
When I write this base64 string to a file I get the exact same string in the file. However, if I use the atob() function to decode it, I get a corrupted file. When I open this file in notepad, I can see that more than 99% of the file is the same as the original. Only a few characters are different. Does somebody know a solution to this?
The File is uploaded via a web portal to the PHP framework. When we use the PHP code to get the file and decode the base64 string to a file, it works just fine. (PHP framework returns in UTF-8).
$http({method: "GET", url: "PATH TO SERVER" + students[y].id + ".json"}).then (function(result) {
$cordovaFile.writeFile(cordova.file.externalCacheDirectory, fileName + result.data.data.data(filetype), result.data.data.base64, true).then(function(res) {
console.log(res);
});
})

Node.js: PDFkit write to response

I'm using PDFKit (A PDF generation library for Node.js, pdfkit.org) and I want to send a PDF as a response to a client.
# Write the PDF file to disk
doc.write('output.pdf');
The code above writes the PDF file to the disk, but I want it to send as the response. How can I do that?
Assuming res is your server response object, just do this:
doc.output(function(string) {
res.end(string);
});
This will send a string representation of the PDF rather than writing it to file. The code above is the compiled CoffeeScript that was in the documentation for PDFKit.
doc.output (string) ->
console.log string

Saving AJAX stream to a PDF file

I'm trying to save the result of a XMLHTTPRequest to a PDF file.
I am communicating with a server I am calling to get a chunk of data formatted as PDF data.
I'm using XMLHTTPRequets to log into the server, then make a search request which in return creates a PDF which is streamed back to me though the XMLHTTPRequest.
I need to save that result as a PDF so I can later open it in Acrobat.
When I save the response text to a file the result is not a valid PDF. The request is doing something to the stream which makes it invalid as a PDF.
I have no control over that server so I cant make it send back a link to a temporary valid PDF file.
Is there a way around that ?
Is there a way to encode that stream into a valid PDF file ?
I am using Javascript for that application.
Thanks
Erez
what you could do is return from your http server an xml file like the following:
<resp><![CDATA[YOURPDFSTREAM]]></resp>
and them retrieve the RESP node to build your pdf file.
Don't forget to encode your stream in the desired encoding format as well.

Categories

Resources