简体   繁体   English

图像名称未在Rails中使用CarrierWave保存为原始图像名称

[英]image name is not saved as original image name using CarrierWave in rails

I am using CarrierWave to upload images which will store into public -> uploads folder. 我正在使用CarrierWave上传将存储到公共-> uploads文件夹中的图像。

In my uploader.rb file, code structure is to give filename is like this, 在我的uploader.rb文件中,代码结构就是这样给文件名,

def filename
"image.#{File.extname(original_filename).downcase}" if original_filename
end

But all images saved as images.jpg. 但是所有图像都另存为images.jpg。 But i want to save image with original name like if i will upload image which contain name like car.jpg than image would be saved as car.jpg not as image.jpg in public -> uploads folder. 但是我想保存原始名称的图像,例如是否要上传包含名称为car.jpg的图像,而不是将图像另存为car.jpg而不是在公共->上传文件夹中保存为image.jpg。

If any one knows about this issue, please help me. 如果有人知道此问题,请帮助我。

Thanks. 谢谢。

You could do something like: 您可以执行以下操作:

def filename
    model.file_name
end

where file_name is the attribute on whatever you are uploading that contains the filename (if you want the extension, append it like you already do). 其中file_name是包含文件名的任何正在上传的属性(如果需要扩展名,请像已经做的那样附加它)。

In your current code the file will be saved as 'image' (because you hardcoded "image.". You could also try 在您当前的代码中,文件将另存为“图片”(因为您对“图片”进行了硬编码。您也可以尝试

"#{original_filename}.#{File.extname(original_filename).downcase}" if original_filename

if original_filename is what you are expecting it to be). 如果original_filename是您期望的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM