- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我知道打开一个文件只会创建一个文件处理程序,无论文件大小如何,它都会占用固定的内存。Django 有一个名为 InMemoryUploadedFile
的类型,它表示通过表单上传的文件。
我像这样在 Django View 中获取我的文件对象的句柄:
file_object = request.FILES["uploadedfile"]
此 file_object 的类型为 InMemoryUploadedFile
。
现在我们可以自己看到,file_object 有方法.read()
用于将文件读入内存。
bytes = file_object.read()
kiểuInMemoryUploadedFile
củafile_object 不是已经“在内存中”了吗?
câu trả lời hay nhất
文件对象的 read()
方法是一种从文件对象中访问内容的方法,无论该文件是在内存中还是存储在磁盘上。它类似于其他实用程序文件访问方法,如 readlines
hoặc seek
。
行为类似于 built into Python 又是基于操作系统的 fread()
方法构建的。
Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately. (For certain files, like ttys, it makes sense to continue reading after an EOF is hit.) Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. Also note that when in non-blocking mode, less data than was requested may be returned, even if no size parameter was given.
VềInMemoryUploadedFile
究竟存储在哪里的问题,它是一个bit more complicated。 .
Before you save uploaded files, the data needs to be stored somewhere.
By default, if an uploaded file is smaller than 2.5 megabytes, Django will hold the entire contents of the upload in memory. This means that saving the file involves only a read from memory and a write to disk and thus is very fast.
However, if an uploaded file is too large, Django will write the uploaded file to a temporary file stored in your system’s temporary directory. On a Unix-like platform this means you can expect Django to generate a file called something like /tmp/tmpzfp6I6.upload. If an upload is large enough, you can watch this file grow in size as Django streams the data onto disk.
These specifics – 2.5 megabytes; /tmp; etc. – are simply “reasonable defaults”. Read on for details on how you can customize or completely replace upload behavior.
关于python - InMemoryUploadedFile 真的是 "in memory"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20827939/
我有一个用户将文件上传到网站,我需要解析电子表格。这是我的代码: input_file = request.FILES.get('file-upload') wb = xlrd.open_workbo
有谁知道如何将 Django2 中上传文件( InMemoryUploadedFile )的内容转换为字符串? 我想知道下面的怎么写convert2string() : uploaded_file =
我有一个从输入字段中选择并通过 Django 中的表单发布的图像文件。 我要发布到另一台服务器的表单数据和图像. 问题好像是,请求中的文件数据是一个InMemoryUploadedFile类型,需要和
我正在尝试在 Django 中发送带有附件的电子邮件。文件是 request.FILE['file']对象(InMemoryUploadedFile 类型)。我通过 EmailMessage(...)
我在将图像从 Django 上传到 S3 存储桶时遇到问题 我收到的错误是: 'InMemoryUploadedFile' object has no attribute 'get' 我看过类似的问题
我有一个 InMemoryUploadedFile 对象,当我对其进行 .read() 时,它会丢失其内容。 是否有可能从同一个对象中两次读取此内容?我试着 .copy() 它,但当然那不应该工作。
我正在 try catch 与表单一起发送的文件并在保存之前对其执行一些操作。所以我需要在临时目录中创建这个文件的副本,但我不知道如何访问它。 Shutil 的函数无法复制此文件,因为它没有路径。那么
我有一个模板,用户可以在其中选择并从他们的计算机上传文件。该文件最终会上传到 S3,但首先会使用自定义验证来验证文件的某些内容。为了检查内容,脚本读取 forms.py 中文件的行: from io
我让用户上传一个 txt 文件,然后提交它,这样我就可以在我的一个 View 中对该文件进行一些处理。 我遇到了一些问题,但在社区的帮助下,现在至少可以识别我表单中的 POST 方法。 发生的事情是我
我有一个在 Eclipse 上使用 Python2.7 和 Django1.2 的 Google Appengine 项目,它允许用户使用表单上传图片、调整图片大小并将其存储为 BLOB 字段。 我在
我的程序是在 Django 中。我从js请求中获取上传的文件。FILES: my_docs = {} for doc_title in request.FILES: doc_name = re
我知道打开一个文件只会创建一个文件处理程序,无论文件大小如何,它都会占用固定的内存。Django 有一个名为 InMemoryUploadedFile 的类型,它表示通过表单上传的文件。 我像这样在
我已阅读所有 documentation对于 TemporaryUploadedFiles 和 InMemoryUploadedFiles,但他们从不谈论清理。我知道 Python 临时文件需要关闭才
我有一个 django 表单,其中有一个接受用户简历的 FileField。我稍后会将简历转换为 html 文档。所以我想到立即对原始文档进行pickle并在其中存储一个数据库列,然后将其unpick
我有以下模型: class IdentifierImage(models.Model): super = models.ForeignKey(Super) identifier = m
我有一个简单的上传表单,其中包含一个图像 FileField : def post(request): if request.user.is_authenticated():
我一直在尝试 help(django.db.models.ImageField)和 dir(django.db.models.ImageField) ,寻找如何创建 ImageField来自上传图像的
所以我正在尝试制作一个包含一些数据和上传字段的表单。 Django 文档没有提供任何在没有 forms.py 的情况下执行此操作的好的教程。我不想用它。 我尝试在我的项目中使用 forms.py (
在 Django Rest 框架中,我想将作为 InMemoryUploadedFile 接收的文件在收到后立即发布到不同的服务器。 听起来很简单,但是 request.post() 函数似乎无法正确
我正在尝试将文件从 postman 上传到 s3 并在k.set_contents_from_filename(文件)类型错误:无效文件:你能看看吗?非常感谢。 序列化器.py from rest_f
Tôi là một lập trình viên xuất sắc, rất giỏi!