Paperclip按用户重命名上传的文件

时间:2022-02-07 10:31:38

Is it possible to allow the user to rename the uploaded file?

是否允许用户重命名上传的文件?

If there is a share link, will it be automatically updated. I am not able to do this since i cant first figure out how to rename the file.

如果有共享链接,会自动更新吗?我不能这样做,因为我不能先弄清楚如何重命名文件。

1 个解决方案

#1


7  

You can rename the files and then change the record file name. For instance, based on this answer, you can do:

您可以重命名文件,然后更改记录文件名。例如,根据这个答案,你可以做:

(record.image.styles.keys+[:original]).each do |style|
    path = record.image.path(style)
    FileUtils.move(path, File.join(File.dirname(path), new_file_name))
end

record.image_file_name = new_file_name
record.save

If you're using Amazon S3, you can do:

如果您使用Amazon S3,您可以这样做:

AWS::S3::S3Object.move_to record.image.path(style), new_file_path, record.image.bucket_name

Check this out: Paperclip renaming files after they're saved

检查一下:在文件被保存后重新命名文件

#1


7  

You can rename the files and then change the record file name. For instance, based on this answer, you can do:

您可以重命名文件,然后更改记录文件名。例如,根据这个答案,你可以做:

(record.image.styles.keys+[:original]).each do |style|
    path = record.image.path(style)
    FileUtils.move(path, File.join(File.dirname(path), new_file_name))
end

record.image_file_name = new_file_name
record.save

If you're using Amazon S3, you can do:

如果您使用Amazon S3,您可以这样做:

AWS::S3::S3Object.move_to record.image.path(style), new_file_path, record.image.bucket_name

Check this out: Paperclip renaming files after they're saved

检查一下:在文件被保存后重新命名文件