如何将 InMemoryUploadedFile 对象复制到磁盘
我试图捕获通过表单发送的文件,并在保存之前对其执行一些操作。所以我需要在临时目录中创建该文件的副本,但我不知道如何访问它。 Shutil 的函数无法复制该文件,因为没有该文件的路径。那么有没有办法以其他方式执行此操作?
我的代码:
image = form.cleaned_data['image']
temp = os.path.join(settings.PROJECT_PATH, 'tmp')
sourceFile = image.name # without .name here it wasn't working either
import shutil
shutil.copy(sourceFile, temp)
引发:
异常类型:IOError at /
异常值:(2,'没有这样的文件或目录')
和调试:
# (..)\views.py in function
67. sourceFile = image.name
68. import shutil
69. shutil.copy2(sourceFile, temp) ...
# (..)\Python26\lib\shutil.py in copy2
92. """Copy data and all stat info ("cp -p src dst").
93.
94. The destination may be a directory.
95.
96. """
97. if os.path.isdir(dst):
98. dst = os.path.join(dst, os.path.basename(src))
99. copyfile(src, dst) ...
100. copystat(src, dst)
101.
▼ Local vars
Variable Value
dst
u'(..)\\tmp\\myfile.JPG'
src
u'myfile.JPG'
# (..)\Python26\lib\shutil.py in copyfile
45. """Copy data from src to dst"""
46. if _samefile(src, dst):
47. raise Error, "`%s` and `%s` are the same file" % (src, dst)
48.
49. fsrc = None
50. fdst = None
51. try:
52. fsrc = open(src, 'rb') ...
53. fdst = open(dst, 'wb')
54. copyfileobj(fsrc, fdst)
55. finally:
56. if fdst:
57. fdst.close()
58. if fsrc:
▼ Local vars
Variable Value
dst
u'(..)\\tmp\\myfile.JPG'
fdst
None
fsrc
None
src
u'myfile.JPG'
I am trying to catch a file sent with form and perform some operations on it before it will be saved. So I need to create a copy of this file in temp directory, but I don't know how to reach it. Shutil's functions fail to copy this file, since there is no path to it. So is there a way to do this operation in some other way ?
My code :
image = form.cleaned_data['image']
temp = os.path.join(settings.PROJECT_PATH, 'tmp')
sourceFile = image.name # without .name here it wasn't working either
import shutil
shutil.copy(sourceFile, temp)
Which raises :
Exception Type: IOError at /
Exception Value: (2, 'No such file or directory')
And the debug :
# (..)\views.py in function
67. sourceFile = image.name
68. import shutil
69. shutil.copy2(sourceFile, temp) ...
# (..)\Python26\lib\shutil.py in copy2
92. """Copy data and all stat info ("cp -p src dst").
93.
94. The destination may be a directory.
95.
96. """
97. if os.path.isdir(dst):
98. dst = os.path.join(dst, os.path.basename(src))
99. copyfile(src, dst) ...
100. copystat(src, dst)
101.
▼ Local vars
Variable Value
dst
u'(..)\\tmp\\myfile.JPG'
src
u'myfile.JPG'
# (..)\Python26\lib\shutil.py in copyfile
45. """Copy data from src to dst"""
46. if _samefile(src, dst):
47. raise Error, "`%s` and `%s` are the same file" % (src, dst)
48.
49. fsrc = None
50. fdst = None
51. try:
52. fsrc = open(src, 'rb') ...
53. fdst = open(dst, 'wb')
54. copyfileobj(fsrc, fdst)
55. finally:
56. if fdst:
57. fdst.close()
58. if fsrc:
▼ Local vars
Variable Value
dst
u'(..)\\tmp\\myfile.JPG'
fdst
None
fsrc
None
src
u'myfile.JPG'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是类似的问题,可能会有所帮助。
This is similar question, it might help.
正如 @Sławomir Lenart 提到的,当上传大文件时,您不希望
data.read()
堵塞系统内存。来自 Django 文档 :
这会将文件保存在
MEDIA_ROOT/tmp/
中,就像您的default_storage
一样,除非另有说明。As mentioned by @Sławomir Lenart, when uploading large files, you don't want to clog up system memory with a
data.read()
.From Django docs :
This will save the file at
MEDIA_ROOT/tmp/
as yourdefault_storage
will unless told otherwise.这是使用 python 的 mkstemp 的另一种方法:
Here is another way to do it with python's
mkstemp
:使用
default_storage
(如在此答案中提到的)不是一个好的选择(至少对我来说) !)。我宁愿使用 <直接代码>FileSystemStorage。注意:
位置
可以是适合目录路径的任何值。它甚至可以是/tmp
。使用
Django 4.1 测试的解决方案
Using
default_storage
(as mentioned in this answer) is not a good option (at least to me!). I would rather useFileSystemStorage
directly.Notes:
The
location
can be any value that suites for a directory path. It can even be a/tmp
.Solution tested with
Django 4.1
您最好的做法是编写自定义上传处理程序。请参阅文档 。如果添加“file_complete”处理程序,则无论有内存文件还是临时路径文件,都可以访问文件的内容。您还可以使用“receive_data_chunck”方法并在其中写入您的副本。
问候
Your best course of action is to write a custom Upload handler. See the docs . If you add a "file_complete" handler, you can access the file's content regardless of having a memory file or a temp path file. You can also use the "receive_data_chunck" method and write your copy within it.
Regards
这就是我尝试在本地保存文件的方法
This is how I tried to save the file locally