Reputation: 29
I received string representating a bytes array to a web service :
"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL ... "
I want to know how to convert the bytes array to image bin file. Are there any gems to do that or do i need to manipulate it with File library.
I read some examples but not great solutions in Ruby.
Do i need first to convert string into bytes array and after file ? And what is the extention that i have to use ?
Thanks a lot.
Upvotes: 0
Views: 3853
Reputation: 2055
Answer to the 1st part of your question Your input looks like base64. so I'll assume you need to first decode from base64:
binary_data = Base64.decode64(data_from_web_service)
File.open('file_name', 'wb') {|f| f.write(binary_data)}
The answer to your 2nd part (how to detect the file extension) is the trickier part. Doesn't the web service return any information about this? If not, you may be successfull by analysing the magic number of the data.
Upvotes: 2
Reputation: 19948
Can you not just write the string to a file:
File.open('picture.jpg', 'w') { |file| file.puts(string) }
Upvotes: -1