如何将InMemoryUploadedFile对象复制到磁盘 [英] How to copy InMemoryUploadedFile object to disk
问题描述
我试图抓住一个表单发送的文件,并在保存之前执行一些操作。所以我需要在temp目录中创建一个这个文件的副本,但是我不知道如何达到它。 Shutil的功能无法复制该文件,因为它没有路径。所以有办法以其他方式执行此操作?
我的代码:
image = form.cleaned_data ['image']
temp = os.path.join(settings.PROJECT_PATH,'tmp')
sourceFile = image.name#without .name here它不工作
import shutil
shutil.copy(sourceFile,temp)
哪个引发:
异常类型:IOError在/
异常值:(2,'没有这样的文件或目录')
而调试:
#(..)\views.py在函数
67. sourceFile = image.name
68. import shutil
69. shutil.copy2(sourceFile ,temp)...
#(..)\Python26\lib\shutil.py在copy2
92.复制数据和所有统计info(cp -p src dst)
93.
94.目的地可能是一个目录
95.
96.
97如果os.path.isdir(dst):
9 8. dst = os.path.join(dst,os.path.basename(src))
99. copyfile(src,dst)...
100. copystat(src,dst)
101.
▼本地vars
变量值
dst
u'(..)\\tmp\\myfile.JPG'
src
u'myfile.JPG'
#(..)\Python26\lib\shutil.py在copyfile
45.复制数据从src到dst
46.如果_samefile(src,dst):
47. raise错误,%s和%s是同一个文件%(src, dst)
48.
49. fsrc =无
50. fdst =无
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.如果fsrc:
▼本地vars
变量值
dst
u'(..) \\tmp\\myfile.JPG'
fdst
无
fsrc
无
src
u'myfile.JPG'
这个是类似的问题,可能有帮助。
从django.core导入os
。 files.storage import default_storage
from django.core.files.base import ContentFile
from django.conf import settings
data = request.FILES ['image']#或self .files ['image']在您的表单
path = default_storage.save('tmp / somename.mp3',ContentFile(data.read()))
tmp_file = os.path .join(settings.MEDIA_ROOT,path)
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'
This is similar question, it might help.
import os
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
from django.conf import settings
data = request.FILES['image'] # or self.files['image'] in your form
path = default_storage.save('tmp/somename.mp3', ContentFile(data.read()))
tmp_file = os.path.join(settings.MEDIA_ROOT, path)
这篇关于如何将InMemoryUploadedFile对象复制到磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!