Ruby on rails 在Rails中解码Base64编码文件花费的时间太长
我从Android应用程序将图像文件发送到Rails API。我使用以下方法解码图像:Ruby on rails 在Rails中解码Base64编码文件花费的时间太长,ruby-on-rails,ruby,heroku,rails-api,Ruby On Rails,Ruby,Heroku,Rails Api,我从Android应用程序将图像文件发送到Rails API。我使用以下方法解码图像: StringIO.new(Base64.decode64(image[1])) 问题是它需要太多的时间;在heroku上需要更长的时间 有没有其他更快捷、更高效的方法 您也可以将此用于解码base64: # this method for decode base64 code to file def parse_image_data(image[1]) base64_file = image[1
StringIO.new(Base64.decode64(image[1]))
问题是它需要太多的时间;在heroku上需要更长的时间
有没有其他更快捷、更高效的方法
您也可以将此用于解码base64:
# this method for decode base64 code to file
def parse_image_data(image[1])
base64_file = image[1]
ext, string = base64_file.split(',')
ext = MIME::Types[base64_file].first.preferred_extension if ext.include?("base64")
tempfile = Tempfile.new(["#{DateTime.now.to_i}", ".#{ext}"])
tempfile.binmode
tempfile.write Base64.decode64(string)
tempfile.rewind
tempfile
end
需要多长时间?图像的大小是多少?需要多长时间?30秒到1分钟…1MB+我在我的机器上快速检查了这个,对于一个2.5MB的文件来说,这要快得多(亚秒)。你确定问题出在解码上吗?也许是读取文件的原因?是的,转换需要时间………让我附上一张屏幕快照这完全适用于从Vue前端将base64编码的图像获取到Rails后端的ActiveStorage中。谢谢