Elixir/Phoenix文件上传文件夹 [英] Elixir/Phoenix file upload folder

查看:80
本文介绍了Elixir/Phoenix文件上传文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试在网站上设置上传功能.但是,我正在努力保留上传文件的位置.

So I'm trying to set up upload functionality on the website. However, I'm struggling with the location where to keep the uploaded files.

我遵循了phoenix准则,将plug Plug.Static, at: "/files", from: "/media", gzip: false添加到了我的Endpoint文件中,并在项目的根目录中创建了/media文件夹.但是不断出现错误:no route found for GET /files/3-news.jpg (Kz.Router).

I followed the phoenix guidelines, added plug Plug.Static, at: "/files", from: "/media", gzip: false to my Endpoint file, and created the /media folder in the root of the project. But keep getting error: no route found for GET /files/3-news.jpg (Kz.Router) .

我不确定自己在做什么错,也许/media文件夹的位置不正确?我把它放在其他文件夹所在的根目录中,例如web,priv,test等.这是正确的位置吗?否则该路径必须是完整的操作系统路径,例如/Users/Jack/Desktop/myApp/media/?

I'm not sure what I'm doing wrong, maybe the location of /media folder is incorrect? I've put it into the root, where others folders are, such as web, priv, test, etc. Is it the right place? Or the path has to be full OS path, like /Users/Jack/Desktop/myApp/media/ ?

在此先感谢大家,感谢任何忠告!

Thanks in advance guys, and any advise appreciated!

P.S.我可以使用File.cp(upload.path, "media/#{file_name}")上传文件,它们显示在/media中,但仍然无法通过http://localhost:4000/files/4-news.jpg

P.S. I'm able to upload files with File.cp(upload.path, "media/#{file_name}") and they appear in /media, but still can't access them via http://localhost:4000/files/4-news.jpg

推荐答案

只需弄清楚就可以了.我的错误是将plug Plug.Static, at: "/files", from: "media/"放置在Entpoint文件的底部,但应将其放置在现有的Plug.Static下,如下所示:

Just figured it out. My mistake was placing plug Plug.Static, at: "/files", from: "media/" at the bottom of Entpoint file, but it should be placed under the existing Plug.Static, like this:

plug Plug.Static,
at: "/", from: :myApp, gzip: false,
only: ~w(css fonts images js favicon.ico robots.txt)

plug Plug.Static, at: "/files", from: "media/"

或者至少在plug myApp.Router之前!然后,您可以访问所需的所有文件.

Or at least before the plug myApp.Router! Then you can access all the files you need.

这篇关于Elixir/Phoenix文件上传文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆