gpt4 book ai didi

javascript - 重命名上传的文件 Ruby On Rails

转载 作者:行者123 更新时间:2023-11-30 17:43:13 27 4
gpt4 key购买 nike

我使用 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>

还有我的 Controller #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() 将获取第一个输入值。

关于javascript - 重命名上传的文件 Ruby On Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20638184/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com