where from does paperclip get the name of original file?

烂漫一生 提交于 2019-12-22 03:50:08

问题


i started using nginx upload module (which creates upload files like /tmp/000121545) but i need paperclip to use original filename while saving files (like /public/avatars/LuckyLuke.jpg)

previously in the parameters Rails were passing just

 "avatar"=>#<File:/tmp/RackMultipart20100413-6151-t3ecq0-0> 

no original filename as well, so i am wondering where from does it come in paperclip? i tried looking through plugin code but it's currently a bit too complex for me.


回答1:


The browser sends a http header with the file name. ("Content-Disposition: filename=original_file.jpg")

Rails makes this available as a instance method of the temp file object: params[:avatar].original_filename, and paperclip uses that.

In detail, Rack parses the multipart form in Rack::Utils::Multipart::UploadedFile and puts a hash in the parameters that includes :tempfile and :filename. Then ActionDispatch::Http::Upload comes along and replaces that hash by the File object (value of :tempfile), extending it with the module ActionDispatch::Http::UploadedFile, which adds a instance variable for original_path and the method original_filename.



来源:https://stackoverflow.com/questions/2631056/where-from-does-paperclip-get-the-name-of-original-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!