Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Base64从Ruby on Rails生成的图像已损坏_Ruby On Rails_File_Base64 - Fatal编程技术网

Ruby on rails Base64从Ruby on Rails生成的图像已损坏

Ruby on rails Base64从Ruby on Rails生成的图像已损坏,ruby-on-rails,file,base64,Ruby On Rails,File,Base64,我正在创建一个表单,该表单使用React处理多重上传,因此我有base64字符串,希望“转换回”图像。 为了在表单提交之前和之后对其进行测试,我可以保证base64没有损坏 我发现了一些关于如何用Rails从base64重新创建图像的相关问题,但我不得不对其进行一些调整,现在我生成的图像被破坏了 我是这样处理的: def portfolio_params params.require(:portfolio).permit(:title, :creation_time,

我正在创建一个表单,该表单使用React处理多重上传,因此我有base64字符串,希望“转换回”图像。 为了在表单提交之前和之后对其进行测试,我可以保证base64没有损坏

我发现了一些关于如何用Rails从base64重新创建图像的相关问题,但我不得不对其进行一些调整,现在我生成的图像被破坏了

我是这样处理的:

def portfolio_params
    params.require(:portfolio).permit(:title, :creation_time,
                                      :public, :content, { illustrations: [] },
                                      :slug, :thumbnail, :website, :tags)
    port_params[:illustrations] = parse_image_data(port_params[:illustrations]) if port_params[:illustrations]
    port_params
  end

def parse_image_data(base64)
    require 'fileutils'
    filename = 'portfolio-file-'
    path = 'public/uploads/portfolio/' + Time.now.strftime('%d%m%Y%H%i%s')

    # Check if directory exists, creates it if not
    FileUtils.mkdir_p(path) unless File.directory?(path)

    # creation of the response variable
    response = []

    # Since I'm getting an array (multiupload), I have to loop through it
    base64.each_with_index do |b, i|

      # Extracts the file format (png, jpg, jpeg..)
      _in_content_type, format, _encoding, _string = b.split(/[:\/;,]/)[1..4]

      # Creates the file with the index so the file doesn't get rewritten
      File.open(path + '/' + filename + i.to_s + '.' + format, 'wb') do |f|

        # Fills it with the decoded base64 string
        f.write(Base64.decode64(b))
      end

      # Response made to the params so that the image path gets saved instead of the base64
      response.push(path + '/' + filename + i.to_s + '.' + format)
    end

  response
end
从理论上讲,一切正常:我的数据库保存路径,创建文件,但是。。。不正确

我不确定我在做什么,我哪里做错了

提前感谢您

对于那些可能(不太可能)遇到同样麻烦的人,问题在于“base64”前缀,如下所示:

data:image/gif;base64,
方法
Base64无法识别。decode64
,仅在之后是

因此,我更改了以下代码:

base64.each_with_index do |b, i|
  _in_content_type, format, _encoding, _string = b.split(/[:\/;,]/)[1..4]

  File.open(path + '/' + filename + i.to_s + '.' + format, 'wb') do |f|
    f.write(Base64.decode64(b.partition('base64,').last))
  end

  response.push(path + '/' + filename + i.to_s + '.' + format)
end
代码
b.partition('base64')
创建一个数组,其中包含分区参数中我pu前后的所有内容