简体   繁体   English

如何将文件从 froala 编辑器上传到 Rails 活动存储

[英]how to upload a file from froala editor to rails active storage

I have a rails 6 app using ActiveStorage (image attached to article) and froala editor.我有一个使用 ActiveStorage(图片附加到文章)和 froala 编辑器的 rails 6 应用程序。 Both upload to S3.两者都上传到 S3。

But I currently use the froala S3 upload and active storage.但我目前使用的是 froala S3 上传和主动存储。 In the form, I have:在表格中,我有:

  1. an upload button, that allows uploading a pdf file and上传按钮,允许上传 pdf 文件和
  2. a text field with froala where images can be uploaded带有 froala 的文本字段,可以在其中上传图像

Active Storage主动存储

class Article < ApplicationRecord
  belongs_to :author
  has_one_attached :image
#ArticleController
@article.image.attach(params[:image])

Froala:弗拉拉:

# class Adm::ArticleController < ApplicationController
    def new 
        #....
        @aws_data = FroalaEditorSDK::S3.data_hash(Article.froalaoptions)
    end
end

and initialise in javascript:并在 javascript 中初始化:

var editor = new FroalaEditor('#article_contenu',{
                              attribution: false,
                              height: 330,
                              key: "mykey",
                              iframeStyleFiles: ['my.css'],
                              pluginsEnabled: ['image'],
                              imageUploadToS3: <%= @aws_data.to_json.html_safe %>,
                              saveURL: '<%= adm_auteur_article_autosave_url %>',
                              saveMethod: 'POST'     
});

I would like to do something like:我想做类似的事情:

var editor = new FroalaEditor('#article_contenu',{
                              attribution: false,
                              height: 330,
                              key: "mykey",
                              iframeStyleFiles: ['my.css'],
                              pluginsEnabled: ['image'],
                              // imageUploadToS3: <%= @aws_data.to_json.html_safe %>,
                              saveURL: '<%= rails_blob_path(article.image, disposition: "attachment")%>    
});

Is there a way to use Rails Active Storage to upload files in the froala editor, similar to Rails ActionText where an image uploaded in the trix editor is stored in ActiveStorage?有没有办法使用 Rails Active Storage 在 froala 编辑器中上传文件,类似于 Rails ActionText,其中在 trix 编辑器中上传的图像存储在 ActiveStorage 中?

From what I've found trying to use Froala with Rails 6 (use something else if you can), in your controller, you can do something like this:从我发现尝试将 Froala 与 Rails 6 一起使用(如果可以的话,使用其他东西),在您的 controller 中,您可以执行以下操作:

blob = ActiveStorage::Blob.create_after_upload!(
  io: params[:file].original_filename,
  filename: params[:file].original_filename,
  content_type: params[:file].content_type
)
render json: { link: url_for(blob), data: blob.id }.to_json

This will create the Blob and and Froala will put the data attribute in the element so you can reference it when you need to purge it by listening for the Froala image.removed event:这将创建 Blob,并且 Froala 会将 data 属性放入元素中,以便您在需要通过侦听 Froala image.removed 事件来清除它时引用它:

    events: {
      'image.removed': function ($img) {
        $.post('/your_delete_file_action', {
          blob_id: $img.attr('data')
        });
     }

This has issues though.不过这有问题。 These blobs won't be attached to anything, so you can't use the unattached method to clean up S3.这些 blob 不会附加到任何东西,因此您不能使用 unattached 方法清理 S3。 This can become a problem if a user uploads an image, but doesn't submit the form or uses the undo button to undo uploading an image.如果用户上传了图像,但没有提交表单或使用撤消按钮撤消上传图像,这可能会成为问题。 It will still be stored on S3.它仍将存储在 S3 上。 Also, if the user deletes an image and then clicks undo, you'll end up with a broken link after the image is deleted from S3.此外,如果用户删除图像然后单击撤消,则在从 S3 中删除图像后,您最终会看到断开的链接。 I'm still trying to work that part out since I have to use Froala for a project.由于我必须将 Froala 用于项目,因此我仍在尝试解决这部分问题。

Edit 5/25/21 So I ended up creating a new model for the froala images and made it has_one_attached.编辑 5/25/21所以我最终为 froala 图像创建了一个新的 model 并将其设置为 has_one_attached。 In the controller, I created a record in that new model's table with the image attached to it.在 controller 中,我在该新模型的表中创建了一条记录,并附有图像。 I added a slug column to the new model and I returned the slug back to froala after an image was uploaded.我在新的 model 中添加了一个 slug 列,并在上传图像后将 slug 返回给 froala。 Since the slug was contained in the element, I sent it to the controller to find and delete the the record that the image was attached to when removed from the editor.由于 slug 包含在元素中,我将其发送到 controller 以查找并删除从编辑器中删除图像时附加到的记录。 Rails then handles purging the records.然后 Rails 处理清除记录。 Since ActiveStorage was already set up with S3, it automatically used it for storing the images.由于 ActiveStorage 已经设置了 S3,它会自动使用它来存储图像。 Still working on solutions for undo/redo and unsubmitted forms with images.仍在为撤消/重做和未提交的 forms 图像解决方案工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 4-如何在带有载波的froala编辑器中上传图像? - Rails 4 - How to have image upload in froala editor with carrierwave? 如何在Rails 5中使用存储上载没有活动记录的文件? - How to use storage to upload a file without active record in Rails 5? Rails Active Storage:从 model 直接上传 - Rails Active Storage: Direct upload from model 如何使用rails的Active Storage将图像上传到S3 <canvas /> 以铁轨形式? - How can I upload an image to S3 with rails' Active Storage from a <canvas /> in a rails form? Rails 6 Active storage sidekiq 文件在 S3 上上传背景图片 - Rails 6 Active storage sidekiq file upload background image on S3 如何使用 Rails 和 Active Storage 实现 AWS S3 分段上传? - How to implement AWS S3 Multipart Upload with Rails and Active Storage? Active Storage 文件上传回调 - Callback for Active Storage file upload 使用 Javascript 上传到 Rails 活动存储 - Upload to Rails Active Storage Using Javascript Active Storage Direct Upload 正在使用简单表单的上传期间删除文件输入 - Rails 6 - Active Storage Direct Upload is removing the file input during upload w/Simple Form - Rails 6 通过 API 到 postman 使用 Rails 主动存储上传文件(.pdf、.jpg 等)? (不通过 Rails 视图) - upload a file(.pdf, .jpg etc) using rails active storage via API through postman? (not through rails views)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM