我在我的应用程序Carrierwave中使用上传声音。
但是我有一个错误,如果我选择上传的声音,但这是错误的声音,如“toto.mp3”,所以我重新选择其他声音,如“titi.mp3”。当我现在上传我的声音时,我的应用程序中的文件是第一个选择的声音,所以是“toto.mp3”,但我选择“titi.mp3”而不是“toto.mp3”。
如果你能提供帮助,我不知道如何解决这个bug。
上传
# encoding: utf-8
require 'carrierwave/processing/mime_types'
class SoundUploader < CarrierWave::Uploader::Base
include CarrierWave::MimeTypes
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"#{Rails.root}/tmp/files/#{model.user_id}/#{model.id}"
end
def extension_white_list
%w(3ga 3gp 3g2 3gpp 3gp2 m4a m4b m4p m4v m4r mp4 aac flac flv avi asf wma wmv dpx mkv mka mks bwf mpg mpeg mp1 mp2 mp3
m1v m1a m2a mpa mpv rm mov ogm ogg ogv oga ogx nut riff webm weba wav mxf asx ts aiff aif aifc au snd caf)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
def filename
model.title = original_filename if model.title.to_s == ''
"#{secure_token}.#{file.extension}" if original_filename.present?
end
protected
def secure_token
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
end
end
我的Javascript:
$(function () {
$('#new_sound').fileupload({
dataType: 'script',
dropZone: $('#new_sound'),
acceptFileTypes: /(3ga|3gp|3g2|3gpp|3gp2|m4a|m4b|m4p|m4v|m4r|mp4|aac|flac|flv|avi|asf|wma|wmv|dpx|mkv|mka|mks|bwf|mpg|mpeg|mp1|mp2|mp3|m1v|m1a|m2a|mpa|mpv|rm|mov|ogm|ogg|ogv|oga|ogx|nut|riff|webm|weba|wav|mxf|asx|ts|aiff|aif|aifc|au|snd|caf)$/i,
limitMultiFileUploads: 1,
add: function (e, data){
var file = data.files[0];
var filetype = $(this).fileupload('option', 'acceptFileTypes');
if (filetype.test(file.name)) {
$('#title').val(file.name);
$('#select_file').css('color', '#00A300');
$('#send_file').css('display', 'inline');
$("#send_file").on('click', function () { data.submit(); });
$("#stop_file").on('click', function () {
$('#progress_file .bar').css('width', '0%');
data.abort();
});
}
else {
alert('Format de fichier non supporté.');
}
},
progress: function (e, data){
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress_file .bar').css('width', progress + '%');
},
});
});
答案 0 :(得分:0)
我发现了这个bug,只是在JS中我删除了行:limitMultiFileUploads:1,现在就可以了。 :)
<强>之前强>
$('#new_sound').fileupload({
dataType: 'script',
dropZone: $('#new_sound'),
acceptFileTypes: /(3ga|3gp|3g2|3gpp|3gp2|m4a|m4b|m4p|m4v|m4r|mp4|aac|flac|flv|avi|asf|wma|wmv|dpx|mkv|mka|mks|bwf|mpg|mpeg|mp1|mp2|mp3|m1v|m1a|m2a|mpa|mpv|rm|mov|ogm|ogg|ogv|oga|ogx|nut|riff|webm|weba|wav|mxf|asx|ts|aiff|aif|aifc|au|snd|caf)$/i,
limitMultiFileUploads: 1,
<强>后强>
$('#new_sound').fileupload({
dataType: 'script',
dropZone: $('#new_sound'),
acceptFileTypes: /(3ga|3gp|3g2|3gpp|3gp2|m4a|m4b|m4p|m4v|m4r|mp4|aac|flac|flv|avi|asf|wma|wmv|dpx|mkv|mka|mks|bwf|mpg|mpeg|mp1|mp2|mp3|m1v|m1a|m2a|mpa|mpv|rm|mov|ogm|ogg|ogv|oga|ogx|nut|riff|webm|weba|wav|mxf|asx|ts|aiff|aif|aifc|au|snd|caf)$/i,