【问题标题】:Ruby - How to set data_uri (base64) filename with ShrineRuby - 如何使用 Shrine 设置 data_uri (base64) 文件名
【发布时间】:2019-01-22 12:00:15
【问题描述】:

我正在尝试将文件名设置为使用带有 FileSystem 存储的 Shrine gem 上传的 base64 文件。

我尝试修改file.metadata[:filename] = 'test.png',但文件名仍然为零。

anex = Anex.new(file_data_uri: file) # file is a base64 string
resource.anexes << anex
resource.save!

神社.rb

require "shrine"
require "shrine/storage/file_system"

Shrine.storages = {
  cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
  store: Shrine::Storage::FileSystem.new("public", prefix: "uploads/store"), # permanent
}

Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data # for forms
Shrine.plugin :data_uri # for base_64
Shrine.plugin :infer_extension
# Shrine.plugin :rack_file # for non-Rails apps

文件上传器.rb

class FileUploader < Shrine
  # plugins and uploading logic

end

Anex.rb

class Anex < ApplicationRecord
  belongs_to :anexable, polymorphic: true

  include FileUploader::Attachment.new(:file) # adds an `file` virtual attribute

  def url
    "#{ENV['FILE_STORAGE_URI']}#{self.file.url}"
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby upload base64 shrine


    【解决方案1】:

    您可以在分配数据 URI 后通过更新 file_data 列来添加文件名:

    anex = Anex.new(file_data_uri: data_uri)
    file = anex.file
    file.metadata["filename"] = "test.png"
    anex.file_data = file.to_json
    

    【讨论】:

    • 谢谢,完美运行。也有办法更改 de id(实际文件名)?
    • 上传位置在Shrine#upload_location中生成,如何覆盖请见github.com/shrinerb/shrine#location
    猜你喜欢
    • 2017-03-23
    • 2013-09-19
    • 2011-10-25
    • 2011-08-08
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2016-11-08
    相关资源
    最近更新 更多