在创建消息之前将录制的视频保存到活动存储中,如何将其附加到消息中?
recorded video saved to active storage before creating message, how can I attach it to message?
我正在使用视频 js 录制插件从浏览器录制视频,然后使用此方法将此视频 blob 保存到活动存储,
def upload_blob
blob = ActiveStorage::Blob.create_after_upload!(
io: params[:blob],
filename: params[:blob].original_filename,
content_type: params[:blob].content_type
)
render json: {url: url_for(blob)}
end
视频的上传发生在用户停止录制和使用此 js 代码创建消息之前
function upload(blob) {
var serverUrl = '/uploader/blob';
var formData = new FormData();
formData.append('blob', blob, blob.name);
console.log('upload recording ' + blob.name + ' to ' + serverUrl);
// start upload
fetch(serverUrl, {
method: 'POST',
body: formData
}).then(
success => console.log('upload recording complete.')
).catch(
error => console.error('an upload error occurred!')
);
}
消息控制器代码如下,
def create
@message = Message.new(message_params)
@message.video.attach(message_params[:video])
@message.video.attach(params[:signed_blob_id])
respond_to do |format|
if @message.save
format.html { redirect_to @message, notice: 'Message was successfully created.' }
format.json { render :show, status: :created, location: @message }
else
format.html { render :new }
format.json { render json: @message.errors, status: :unprocessable_entity }
end
end
结束
如何将上传的视频附加到数据库中的消息条目?
我通过在表单视图中添加一个 heddin 字段来解决它,并且在视频上传完成后,我将创建的 blob 的路径分配给该隐藏字段。
然后在控制器中,您使用File.open将视频附加到消息中
@message = Message.new(message_params)
@message.video.attach(io: File.open(message_params[:video_url]), filename: "#{message_params[:title]}.webm", content_type: "video/webm")
我希望这会对某人有所帮助
我正在使用视频 js 录制插件从浏览器录制视频,然后使用此方法将此视频 blob 保存到活动存储,
def upload_blob
blob = ActiveStorage::Blob.create_after_upload!(
io: params[:blob],
filename: params[:blob].original_filename,
content_type: params[:blob].content_type
)
render json: {url: url_for(blob)}
end
视频的上传发生在用户停止录制和使用此 js 代码创建消息之前
function upload(blob) {
var serverUrl = '/uploader/blob';
var formData = new FormData();
formData.append('blob', blob, blob.name);
console.log('upload recording ' + blob.name + ' to ' + serverUrl);
// start upload
fetch(serverUrl, {
method: 'POST',
body: formData
}).then(
success => console.log('upload recording complete.')
).catch(
error => console.error('an upload error occurred!')
);
}
消息控制器代码如下,
def create
@message = Message.new(message_params)
@message.video.attach(message_params[:video])
@message.video.attach(params[:signed_blob_id])
respond_to do |format|
if @message.save
format.html { redirect_to @message, notice: 'Message was successfully created.' }
format.json { render :show, status: :created, location: @message }
else
format.html { render :new }
format.json { render json: @message.errors, status: :unprocessable_entity }
end
end
结束
如何将上传的视频附加到数据库中的消息条目?
我通过在表单视图中添加一个 heddin 字段来解决它,并且在视频上传完成后,我将创建的 blob 的路径分配给该隐藏字段。
然后在控制器中,您使用File.open将视频附加到消息中
@message = Message.new(message_params)
@message.video.attach(io: File.open(message_params[:video_url]), filename: "#{message_params[:title]}.webm", content_type: "video/webm")
我希望这会对某人有所帮助