erlang file from post param

191 views Asked by At

I have a problem: I need to read file from post param in ChicagoBoss. 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. I'm trying to upload file with Postman. Where is the problem?

2

There are 2 answers

0
John Haugeland On

enoent is the posix error code for "directory not found."

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.

1
Berzemus On

What's inside Req:post_param("file") ?

You assume it's a path to a file: have you checked the value of File ?

Anyway, it's Req:post_files/0 you are probably looking for:

[{_, _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.