3
Operating System: Windows 10;
Version of Ruby: 2.2.1;
Server Application: Apache 2 (mod_cgi);
I must create a script that reads a file, convert it to text and after a possible processing, send the result to the user.
The current process works very well for text files (html, Plain, css etc...), but when the subject is binary (pdf, jpg, png etc...) the files always come out "unreadable" or corrupted (in the case of Jpegs)once the browser can mount the image but it is not the original on the server.
The code used so far is as follows::
mime_type = 'image/jpeg'
file = File.open(File.dirname(__FILE__) + 'imagem.jpg', "rb")
head = "Status: 200 OK\nContent-Type: #{mime_type}\nConnection: close\nContent-Length: #{file.size()}\n\n"
body = file.read()
print head + body
Does anyone know a safe way to read binary files on Windows to return to client?
I tested the above solutions. The image is not yet shown. However, now when downloading it, the resulting file has the exact size of the original file. (Code updated in question)
– Guill
Does using "print head" and "STDOUT.write(body)" change anything? I’m guessing "print" and "puts" are translating some character like CRLF or Control-Z.
– epx
She’s still corrupt, Miss Scarlett. By my analysis the resulting file has the same size in bytes as the original, but has an extra line. (in the case of this specific image) In the case of PDF the file has the same size but thirty lines more.
– Guill
I found out that the nonsense probably happens in
file.read()
. Because I tried to save what was read to a new file and this new file was corrupted and larger than the original.– Guill
It may be that File.read() is trying to apply an encoding. It has the IO.binread(name) method that might be worth trying
– epx
I have tried. The result is the same. I believe that the
File.read()
uses theIO.binread
.– Guill