如何从post param中读取? [英] How to read from post param?

查看:207
本文介绍了如何从post param中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析文本文件。此文件位于post param中。我有这样的代码:

I need to parse text file. This file is in post param. I have code like this:

upload_file('POST', []) ->
    File = Req:post_param("file"),

下一步该怎么办?如何解析?

What should I do next? How to parse it?

推荐答案

内部要求:post_param(file)

您假设它是文件的路径:是否检查了文件的值?

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

无论如何,这是 Req:post_files / 0 你可能正在寻找:

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 temporary location, you'd better find a more suitable place to store them.

似乎 uploaded_file 记录有现在有5个字段(现在10个月)。

It seems the uploaded_file record has 5 fields now (for 10 months by now).

这是更新的例子,第五个字段:

This is the updated example, with the fifth field:

[{_, _FileName, TempLocation, _Size, _Name}|_] = Req:post_files(),
{ok,Data} = file:read_file(TempLocation),

哦,而且因为使用它是一个记录,以下示例应该工作,即使定义再次更新:

Oh, and because it's a record, following example should work even if the definition gets updated once again:

[File|_] = Req:post_files(),
{ok,Data} = file:read_file(File#uploaded_file.temp_file),


$ b $另一个警告:上面的代码,如任何erlanger所看到的,只处理第一个,也许大部分时间只处理上传的文件。如果同时上传更多文件,这些文件将被忽略。

Another warning: the code above, as any erlanger will see, only deals with the first and, probably most of the times, only uploaded file. Should more files be uploaded at the same time, these would be ignored.

这篇关于如何从post param中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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