使用 recorder.js 时如何为 CarrierWave 设置 content_type 和 original_filename
How to set content_type and original_filename for CarrierWave when using recorder.js
我正在使用 recorder.js
和 CarrierWave
实现录制用户语音。
我可以通过这段代码保存声音,但仍然存在一些问题。
function saveSound() {
recorder && recorder.exportWAV(function (blob) {
var url = URL.createObjectURL(blob);
var formData = new FormData();
formData.append('voice[sound]', blob, "foo.wav");
formData.append('voice[phrase_id]', $('#phrase').data('phrase-id'));
$.ajax({
type: "POST",
url: "/voices",
processData: false,
contentType: false,
data: formData
});
});
}
app/models/voice.rb
class Voice < ActiveRecord::Base
belongs_to :phrase
belongs_to :user
mount_uploader :sound, SoundUploader
end
这是 recorder.js
创建的声音。
Voice.last.sound
=> #<SoundUploader:0x007fb5dda13818
@file=#<CarrierWave::SanitizedFile:0x007fb5dda0bb18 @content_type=nil, @file="/Users/ironsand/dev/nativephrase/public/uploads/voice/sound/15/blob", @original_filename=nil>,
@model=
#<Voice:0x007fb5dc549e00
id: 15,
phrase_id: 24,
user_id: nil,
type: nil,
gender: nil,
created_at: Thu, 31 Dec 2015 17:11:48 UTC +00:00,
updated_at: Thu, 31 Dec 2015 17:11:48 UTC +00:00,
sound: "blob">,
@mounted_as=:sound,
@storage=#<CarrierWave::Storage::File:0x007fb5dda12d78 @uploader=#<SoundUploader:0x007fb5dda13818 ...>>,
@versions={}>
所以我在formData
foo.wav
中设置的名字被忽略了。如果我在 ajax 请求中设置 content-type
,我将无法上传声音。
如何设置 CarrierWave 模型的 @content_type
和 @original_filename
?
这对我有用。
class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
include CarrierWave::MimeTypes
process :set_content_type
process :save_content_type
def save_content_type
model.content_type = file.content_type if file.content_type
end
def filename
"#{model.type}_#{model.user_id}.xls" if original_filename
end
end
我正在使用 recorder.js
和 CarrierWave
实现录制用户语音。
我可以通过这段代码保存声音,但仍然存在一些问题。
function saveSound() {
recorder && recorder.exportWAV(function (blob) {
var url = URL.createObjectURL(blob);
var formData = new FormData();
formData.append('voice[sound]', blob, "foo.wav");
formData.append('voice[phrase_id]', $('#phrase').data('phrase-id'));
$.ajax({
type: "POST",
url: "/voices",
processData: false,
contentType: false,
data: formData
});
});
}
app/models/voice.rb
class Voice < ActiveRecord::Base
belongs_to :phrase
belongs_to :user
mount_uploader :sound, SoundUploader
end
这是 recorder.js
创建的声音。
Voice.last.sound
=> #<SoundUploader:0x007fb5dda13818
@file=#<CarrierWave::SanitizedFile:0x007fb5dda0bb18 @content_type=nil, @file="/Users/ironsand/dev/nativephrase/public/uploads/voice/sound/15/blob", @original_filename=nil>,
@model=
#<Voice:0x007fb5dc549e00
id: 15,
phrase_id: 24,
user_id: nil,
type: nil,
gender: nil,
created_at: Thu, 31 Dec 2015 17:11:48 UTC +00:00,
updated_at: Thu, 31 Dec 2015 17:11:48 UTC +00:00,
sound: "blob">,
@mounted_as=:sound,
@storage=#<CarrierWave::Storage::File:0x007fb5dda12d78 @uploader=#<SoundUploader:0x007fb5dda13818 ...>>,
@versions={}>
所以我在formData
foo.wav
中设置的名字被忽略了。如果我在 ajax 请求中设置 content-type
,我将无法上传声音。
如何设置 CarrierWave 模型的 @content_type
和 @original_filename
?
这对我有用。
class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
include CarrierWave::MimeTypes
process :set_content_type
process :save_content_type
def save_content_type
model.content_type = file.content_type if file.content_type
end
def filename
"#{model.type}_#{model.user_id}.xls" if original_filename
end
end