简体   繁体   中英

How can I display an image whose path is outside my Ruby on Rails project directory

如何显示图像,该图像存储在项目目录的外部。

You can do two things: Either you symlink to the file from within your public folder and serve the image as a static asset, or you read the image with File.read(/path/to/file) and send the binary data with send_file or send_data from within your controller.

The first option can also be slightly modified: you could catch the request for the image with a rails controller action and serve it to the browser. Additionally you could add the symlink and have the file served statically from then on.

I hope, this helps.

it is not recommended simply copy the image into your public image directory and use it. However it uses the relative path so it will be difficult for the image outside of the project directory to be displayed but you can upload the image and copy the url and then can use that url but this is also not a good approach.

如果你想从项目目录中提供图像路径,你应该有服务器根目录路径和image_folderpath,例如图像文件夹可以在/ home / ror / image_folder中找到,你可以从那个目录访问

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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