Javascript 用Rails上传文件?

Javascript 用Rails上传文件?,javascript,html,ruby-on-rails,Javascript,Html,Ruby On Rails,我想:点击按钮=>选择一个文件=>点击“确定”(或双击文件)=>自动启动上传 如何使用rails实现这一点?看看回形针gem,假设您使用rails 3,它应该涵盖所有内容 看看回形针宝石,假设您使用rails 3,它应该涵盖所有内容 您可以按照建议使用回形针,但我从您的问题中了解到,您想取消“提交”按钮吗 让javascript侦听文件字段上的onChange事件并提交表单: :onchange => "this.form.submit();" 您可以按照建议使用回形针,但我从您的问

我想:点击按钮=>选择一个文件=>点击“确定”(或双击文件)=>自动启动上传


如何使用rails实现这一点?

看看回形针gem,假设您使用rails 3,它应该涵盖所有内容



看看回形针宝石,假设您使用rails 3,它应该涵盖所有内容



您可以按照建议使用回形针,但我从您的问题中了解到,您想取消“提交”按钮吗

让javascript侦听文件字段上的onChange事件并提交表单:

:onchange => "this.form.submit();"

您可以按照建议使用回形针,但我从您的问题中了解到,您想取消“提交”按钮吗

让javascript侦听文件字段上的onChange事件并提交表单:

:onchange => "this.form.submit();"

控制器

  def file_upload
    begin
      ModelName.file_upload(params[:upload])
      flash[:notice] = "File has been uploaded successfully"
      redirect_to :action => "method_name"
    rescue Exception => e
      flash[:error] = "Error with upload! Please retry."
    end
  end
型号

 def self.file_upload(upload)
    name =  upload.original_filename
    directory = "#{DIR_PATH}"
    # create the file path
    path = File.join(directory, name)
    # write the file
    File.open(path, "wb") { |f| f.write(upload.read) }
  end
查看

        <% form_tag({:controller => "controller_name", :action => "file_upload"}, {:multipart => true}) do%>
          <div class="fileinputs">
            <input type="file" name="upload" id="upload" class="file" onchange="this.form.submit();" />
            <div class="fakefile">
              <input class="upload_btn"/>
            </div>
          </div>
        <% end %>

控制器

  def file_upload
    begin
      ModelName.file_upload(params[:upload])
      flash[:notice] = "File has been uploaded successfully"
      redirect_to :action => "method_name"
    rescue Exception => e
      flash[:error] = "Error with upload! Please retry."
    end
  end
型号

 def self.file_upload(upload)
    name =  upload.original_filename
    directory = "#{DIR_PATH}"
    # create the file path
    path = File.join(directory, name)
    # write the file
    File.open(path, "wb") { |f| f.write(upload.read) }
  end
查看

        <% form_tag({:controller => "controller_name", :action => "file_upload"}, {:multipart => true}) do%>
          <div class="fileinputs">
            <input type="file" name="upload" id="upload" class="file" onchange="this.form.submit();" />
            <div class="fakefile">
              <input class="upload_btn"/>
            </div>
          </div>
        <% end %>

谷歌上传,回形针,或Carrierwave;尝试一下,然后带着关于实现Google for Uploadify、回形针或Carrierwave的问题回来;尝试一下,然后带着关于实现的问题回来