簡體   English   中英

重命名上傳的文件Ruby On Rails

[英]Rename file uploaded Ruby On Rails

我使用jquery-multiupload-rails創建多上傳。

這樣就可以了,但是當我想修改文件的標題時,使用的標題對於2個文件是相同的,而不是文件1的標題1和文件2的標題2等。

我使用javascript來做到這一點。

我的表格在javascript中

<%= form_for Sound.new , :html => { :multipart => true, :id => "fileupload"  } do |f| %>
  <!-- Redirect browsers with JavaScript disabled to the origin page -->
  <noscript><input type="hidden" name="redirect" value="http://blueimp.github.io/jQuery-File-Upload/"></noscript>
  <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
  <div class="row fileupload-buttonbar">
    <div class="span7">
      <!-- The fileinput-button span is used to style the file input field as button -->
      <span class="btn btn-success fileinput-button">
        <i class="icon-plus icon-white"></i>
        <span>Add files...</span>
        <%= f.file_field :file, multiple: true, id: 'upload-field' %>
      </span>
      <button type="submit" class="btn btn-primary start">
        <i class="icon-upload icon-white"></i>
        <span>Start upload</span>
      </button>
      <button type="reset" class="btn btn-warning cancel">
        <i class="icon-ban-circle icon-white"></i>
        <span>Cancel upload</span>
      </button>
      <button type="button" class="btn btn-danger delete">
        <i class="icon-trash icon-white"></i>
        <span>Delete</span>
      </button>

<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">
  {% for (var i=0, file; file=o.files[i]; i++) { %}
  <tr class="template-upload fade">
    <td class="preview"><span class="fade"></span></td>
    <td class="name"><span>{%=file.name%}</span></td>
    <td class="title"><input name="title[]" value='{%=file.name%}' required></td>
    <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>

我知道我的錯誤在這一行:如果我使用title [],那么對於2個文件,標題是“ title1,title2”。 如果我僅對2個文件使用標題,則文件1和文件2僅具有“ title2”。

<td class="title"><input name="title[]" value='{%=file.name%}' required></td>

和我的控制器#create

def create
    @sound = Sound.new(params[:sound])
    @sound.title = params[:title].to_s.gsub(/\[|\]|"/,'')
    respond_to do |format|
      if @sound.save
        redirect_to sounds_path
      else
        format.html { render action: "new" }
        format.json { render json: @sound.errors, status: :unprocessable_entity }
      end
    end
  end

安慰

    Started POST "/sounds" for 127.0.0.1 at 2013-12-17 17:59:09 +0100
Processing by SoundsController#create as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ke1wOlf1z+eoQ6cS0lpQraqsiiU0BXoT2VtFweGed18=", "title"=>"309060_4371516803467_1048226528_n.jpg", "sound"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007fa626cbff88 @original_filename="14591_4925922503263_818636474_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"sound[file]\"; filename=\"14591_4925922503263_818636474_n.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/sj/sr703c8n6llc198jfg6746d40000gn/T/RackMultipart20131217-2706-itxsfb>>}}
   (0.1ms)  begin transaction
  SQL (0.6ms)  INSERT INTO "sounds" ("created_at", "file", "name", "title", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Tue, 17 Dec 2013 16:59:09 UTC +00:00], ["file", "14591_4925922503263_818636474_n.jpg"], ["name", nil], ["title", "309060_4371516803467_1048226528_n.jpg"], ["updated_at", Tue, 17 Dec 2013 16:59:09 UTC +00:00]]
   (1.1ms)  commit transaction
Redirected to http://localhost:3000/sounds
Completed 406 Not Acceptable in 14ms (ActiveRecord: 1.8ms)


Started POST "/sounds" for 127.0.0.1 at 2013-12-17 17:59:09 +0100
Processing by SoundsController#create as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ke1wOlf1z+eoQ6cS0lpQraqsiiU0BXoT2VtFweGed18=", "title"=>"309060_4371516803467_1048226528_n.jpg", "sound"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007fa625a8d9a0 @original_filename="25490_392719757235_767862235_3946737_600749_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"sound[file]\"; filename=\"25490_392719757235_767862235_3946737_600749_n.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/sj/sr703c8n6llc198jfg6746d40000gn/T/RackMultipart20131217-2706-d32r2a>>}}
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "sounds" ("created_at", "file", "name", "title", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Tue, 17 Dec 2013 16:59:09 UTC +00:00], ["file", "25490_392719757235_767862235_3946737_600749_n.jpg"], ["name", nil], ["title", "309060_4371516803467_1048226528_n.jpg"], ["updated_at", Tue, 17 Dec 2013 16:59:09 UTC +00:00]]
   (2.0ms)  commit transaction
Redirected to http://localhost:3000/sounds
Completed 406 Not Acceptable in 8ms (ActiveRecord: 2.4ms)

謝謝您幫忙。

 <td class="title"><input name="sound[title]" value='{%=file.name%}' required></td> or <td class="title"><span><input type="text" name="sound[title]"value="{%= file.name.split('/').pop().split('.').shift()%}" required/></span></td> 

將此添加到提交回調中

 $('#fileupload').bind('fileuploadsubmit', function (e, data) { var inputs = data.context.find(':input'); if (inputs.filter('[required][value=""]').first().focus().length) { return false; } data.formData = inputs.serializeArray(); }); 

.first()將采用第一個輸入值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM