Rails upload imageless file to database without using temp server files?

I am exploring the possibility of adding a feature to my Rails-based intranet site that allows users to upload files.

Two goals: My users are widely distributed by geography and do not always work with documents in a shared network storage (different addresses, DNS records, etc. out of my control or interest), so I'm thinking of providing an alternative database. We have several files from which we analyze data on the client side. I would prefer to be able to push this to the server.

I looked at attachment_fu, Paperclip and another (forgot the name!), All of which seem very image-oriented, although attachment_fu can at least work without an image processing library, thank goodness.

The big problem is that my server does not allow my application to write files locally, and these plugins seem to want to create a Tempfile.

Questions (finally!)

Is there a reasonable way to download binary data and process it in memory and / or save it as a BLOB without saving the file on the server side?

Or should I abandon the idea of ​​distributing files and provide users with a second option for copying and pasting text fields, where possible?

(The closest I could find on SO was this , which really doesn't help)

+3
5

, , , , Tempfile.

, , .

Rails tempfiles, 15k .

<%= f.file_field :file %>
....
file_param = params[:upload][:file]

- , 15k, params[:upload][:file] ActionController::UploadedTempFile.

? Rails, , tempfiles temp ( ), , , RAILS_ROOT/tmp, . , , , .

, temp path_path root.. :

Technoweenie::AttachmentFu.tempfile_path = Dir::tmpdir

** PS: . attachment_fu, , , , / temp : -)

+1

params .

, .

<% form_for :upload, :url => {:action=>:upload}, :html=>{:multipart=>true} do |f| %>
  <%= f.file_field :file %>
  <%= f.submit 'Upload' %>
<% end %>

.

class TestController < ApplicationController

  def upload 
    file_param = params[:upload][:file]
    filename = file_param.original_filename
    filedata = file_param.read

    @data = UploadedFile.create(:name => filename, :data => filedata)

    render :text => "created #{@data.id}"
  end

end

, .

class CreateUploadedFiles < ActiveRecord::Migration
  def self.up
    create_table :uploaded_files do |t|
      t.string :name
      t.binary :data
      t.timestamps
    end
  end

  def self.down
    drop_table :uploaded_files
  end
end

, !

+7

HowTo Rails ( ) , . , , BLOB ActiveRecord . , , , , HowTo.

, , , -, .

+1

, :

  def upload
    file_content = params[:upload][:file]
    render :text => [
      [:original_path, :content_type, :local_path, :path, :original_filename].collect {|m| file_content.send(m)},
      file_content.class, 
      file_content.size.to_s].flatten.join("<br/>")
  end

:

b_wib.xls
application/vnd.ms-excel


b_wib.xls
ActionController::UploadedStringIO
13824

:

a_wib.xls
application/vnd.ms-excel
/tmp/CGI.10029.1
/tmp/CGI.10029.1
a_wib.xls
ActionController::UploadedTempfile
27648

..., .

0
source

For those who read this by simply storing the / IO file in the parameters in the database, this is a good solution (why complicate the matter). A clip, and I suspect attachment_fu is not image specific. Since image loading and resizing are very common, Paperclip comes with a processor for resizing images, but it is not enabled by default, and you can easily add your own processors.

0
source

Source: https://habr.com/ru/post/1699602/


All Articles