在 web2py 上传文件 [英] File upload at web2py

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

问题描述

我正在使用 web2py 框架.我已经通过 SQLFORM 上传了一个 txt 文件,该文件存储在上传文件夹"中,现在我需要从控制器读取这个 txt 文件,我应该在 default.py 中定义的函数中使用什么文件路径?

I am using the web2py framework. I have uploaded txt a file via SQLFORM and the file is stored in the "upload folder", now I need to read this txt file from the controller, what is the file path I should use in the function defined in the default.py ?

def readthefile(uploaded_file):
    file = open(uploaded_file, "rb")
    file.read()
    ....

推荐答案

转换后的上传文件名存储在你的数据库表的upload字段中,所以你需要一种方法来查询通过SQLFORM 提交以获取存储文件的名称.下面是假设您知道记录 ID 的情况:

The transformed name of the uploaded file is stored in the upload field of your database table, so you need a way to query the specific record that was inserted via the SQLFORM submission in order to get the name of the stored file. Here is how it would look assuming you know the record ID:

stored_filename = db.mytable(record_id).my_upload_field
original_filename, stream = db.mytable.my_upload_field.retrieve(stored_filename)
stream.read()

当您将文件名传递给上传字段的 .retrieve 方法时,它将返回一个包含原始文件名以及打开文件对象(称为 stream)的元组> 在上面的代码中).

When you pass a filename to the .retrieve method of an upload field, it will return a tuple containing the original filename as well as the open file object (called stream in the code above).

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

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