Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将文件作为附件Django发送_Python_Django_Django 1.11 - Fatal编程技术网

Python 将文件作为附件Django发送

Python 将文件作为附件Django发送,python,django,django-1.11,Python,Django,Django 1.11,我已经创建了一个XLS文件,希望将其作为电子邮件附件发送。以下代码工作正常,我收到一封包含XLS文件的电子邮件(状态代码为200 OK) 但如果我尝试发送压缩的XLS文件(使用zipfile),我不会收到任何东西(但是状态代码是“200OK”)。 我用以下代码替换了最后2行代码: report_name_zip = 'do_something_%(username)s_%(current_date)s.zip' with zipfile.ZipFile(f, mode='w', compre

我已经创建了一个XLS文件,希望将其作为电子邮件附件发送。以下代码工作正常,我收到一封包含XLS文件的电子邮件(状态代码为200 OK)

但如果我尝试发送压缩的XLS文件(使用zipfile),我不会收到任何东西(但是状态代码是“200OK”)。 我用以下代码替换了最后2行代码:


report_name_zip = 'do_something_%(username)s_%(current_date)s.zip'

with zipfile.ZipFile(f, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
    zf.writestr(zinfo_or_arcname=report_name, bytes=f.getvalue())

message.attach(report_name_zip, f.getvalue())
message.send()

您可以对
stringIO
进行读写操作。您应该使用两个独立的
StringIO
s:

report_name_zip = 'do_something_%(username)s_%(current_date)s.zip'

report_name = "do_something_%(username)s_%(current_date)s.xls"

f1 = StringIO.StringIO()
work_book.save(f1)

f2 = StringIO.StringIO()
with zipfile.ZipFile(f2, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
    zf.writestr(zinfo_or_arcname=report_name, data=f1.getvalue())

message.attach(report_name_zip, f2.getvalue())
message.send()
report_name_zip='do_something_%(用户名)s_%(当前日期)s.zip'
report_name=“do_something(用户名)s_uu%(当前日期)s.xls”
f1=StringIO.StringIO()
工作簿保存(f1)
f2=StringIO.StringIO()
zipfile.zipfile(f2,mode='w',compression=zipfile.ZIP_DEFLATED)作为zf:
zf.writestr(zinfo_或_arcname=report_name,data=f1.getvalue())
message.attach(report\u name\u zip,f2.getvalue())

message.send()
您两人读写同一个
StringIO
?应该
zf.writestr(zinfo\u或_arcname=report\u name,bytes=f.getvalue())
没有
zinfo\u或_arcname=report\u name\u zip
?@DanielHolmes:如果我理解正确的话,
report\u name
就是文件名,
报告\u name\u zip
是zipfile的名称。@WillemVanOnsem啊,好的,我现在明白了
report_name_zip = 'do_something_%(username)s_%(current_date)s.zip'

report_name = "do_something_%(username)s_%(current_date)s.xls"

f1 = StringIO.StringIO()
work_book.save(f1)

f2 = StringIO.StringIO()
with zipfile.ZipFile(f2, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
    zf.writestr(zinfo_or_arcname=report_name, data=f1.getvalue())

message.attach(report_name_zip, f2.getvalue())
message.send()