我避免在磁盘上创建文件,这是我到目前为止所得到的:
def get_zip(request):
import zipfile, StringIO
i = open('picture.jpg', 'rb').read()
o = StringIO.StringIO()
zf = zipfile.ZipFile(o, mode='w')
zf.writestr('picture.jpg', i)
zf.close()
o.seek(0)
response = HttpResponse(o.read())
o.close()
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = "attachment; filename=\"picture.zip\""
return response
Run Code Online (Sandbox Code Playgroud)
你认为是正确的 - 优雅 - pythonic足够吗?有更好的方法吗?
谢谢!
我需要上传文件,然后使用回形针对其进行解析?
目前,它是在/ system文件夹中上传的,在heroku中是不允许的。
我不需要上传是持久的...我解析它然后存储它们。
因此,我希望能够保存到/ tmp中,然后进行解析,然后在以后将其删除。
关于如何执行此操作的想法(如果我应该执行此操作)?