[英]erlang file from post param
I have a problem: I need to read file from post param in ChicagoBoss. 我有一个问题:我需要从ChicagoBoss中的post param中读取文件。 I'm trying:
我正在努力:
upload_file('POST', []) ->
File = Req:post_param("file"),
{ok,Data} = file:read_file(File),
And have an error: 并有一个错误:
{{badmatch,{error,enoent}}
When I'm trying to check the file like: 当我尝试检查文件时:
case filelib:is_file(File) of
true -> {output, "ok"};
false -> {output, "error"}
end.
I have error
output. 我有
error
输出。 I'm trying to upload file with Postman. 我正在尝试通过邮递员上传文件。 Where is the problem?
问题出在哪儿?
What's inside Req:post_param("file")
? Req:post_param("file")
里面是什么?
You assume it's a path to a file: have you checked the value of File
? 您假设它是文件的路径:是否检查
File
的值?
Anyway, it's Req:post_files/0
you are probably looking for: 无论如何,您可能正在寻找的
Req:post_files/0
:
[{_, _FileName, TempLocation, _Size}|_] = Req:post_files(),
{ok,Data} = file:read_file(TempLocation),
It's also probably a Bad Idea to leave the file at it's temperory location, you'd better find a more suitable place to store them. 将文件保留在临时位置也是一个坏主意,您最好找到一个更合适的存储位置。
enoent
is the posix error code for "directory not found." enoent
是“找不到目录”的posix错误代码。
http://www.erlang.org/doc/man/file.html#del_dir-1 http://www.erlang.org/doc/man/file.html#del_dir-1
Also, your code allows people to read arbitrary files from disk. 另外,您的代码还允许人们从磁盘读取任意文件。 That will eventually lead to a server attack.
最终将导致服务器攻击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.