I received string representating a bytes array to a web service :
我收到一个字符串表示一个字节数组到Web服务:
"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL ... "
“/ 9j / 4AAQSkZJRABABAQEAYABgAAD / 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.
我想知道如何将bytes数组转换为image bin文件。是否有任何宝石可以做到这一点,或者我需要使用文件库来操作它。
I read some examples but not great solutions in Ruby.
我在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.
非常感谢。
2 个解决方案
#1
2
Answer to the 1st part of your question Your input looks like base64. so I'll assume you need to first decode from base64:
回答问题的第1部分您的输入看起来像base64。所以我假设你需要先从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.
第二部分(如何检测文件扩展名)的答案是棘手的部分。 Web服务是否返回有关此内容的任何信息?如果没有,您可以通过分析数据的幻数来成功。
#2
-1
Can you not just write the string to a file:
你能不能只将字符串写入文件:
File.open('picture.jpg', 'w') { |file| file.puts(string) }
#1
2
Answer to the 1st part of your question Your input looks like base64. so I'll assume you need to first decode from base64:
回答问题的第1部分您的输入看起来像base64。所以我假设你需要先从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.
第二部分(如何检测文件扩展名)的答案是棘手的部分。 Web服务是否返回有关此内容的任何信息?如果没有,您可以通过分析数据的幻数来成功。
#2
-1
Can you not just write the string to a file:
你能不能只将字符串写入文件:
File.open('picture.jpg', 'w') { |file| file.puts(string) }