我需要在Django应用程序中浏览并打开.msg或.eml文件。我使用了forms和request.files,但是流提供了八位字节格式的数据。如何获取原始文件对象并将其作为普通文件打开以便解析它?
我有代码要分享。
<form action="{% url "upload_file_eml" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input class="btn btn-primary btn-large" type="submit" value="Start Single Email Test"/></p>
</form>
documents = handle_uploaded_eml_file(request.FILES)
def handle_uploaded_eml_file(fileslist1):
if(len(fileslist1['docfile']) == 0):
return "No Files"
else:
filename1 = fileslist1['docfile'].name
if(checking_for_extension for emails with .msg or eml):
emlfile = fileslist1['docfile'].file
msg2 = email.message_from_file(emlfile)
//msg2 is in octet format it doesn't parse because file being .msg
//what i kind want to do
//emlfile = open(fileslist1['docfile'].file)
//msg2 = emlfile.read()
print("email subject:" + msg2["Subject"])
return "Upload Success"
else:
return "Unsupported Format"