Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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是否将上载的.doc文件保存为.zip文件?_Python_Django_File_Upload_Docx - Fatal编程技术网

Python Django是否将上载的.doc文件保存为.zip文件?

Python Django是否将上载的.doc文件保存为.zip文件?,python,django,file,upload,docx,Python,Django,File,Upload,Docx,因此,我使用标准上传文件代码在我的网站上创建了一个上传功能。我测试过的所有文件类型(pdf、gif、jpg)似乎都可以正常工作,但当我尝试上载docx文件时,Django将该文件存储为.zip文件。显然这和“内容嗅探”有关?这是密码- def upload_form(request): if request.method == 'POST': # Gets the information about the file uploaded_file = request.FILE

因此,我使用标准上传文件代码在我的网站上创建了一个上传功能。我测试过的所有文件类型(pdf、gif、jpg)似乎都可以正常工作,但当我尝试上载docx文件时,Django将该文件存储为.zip文件。显然这和“内容嗅探”有关?这是密码-

def upload_form(request):
  if request.method == 'POST':

    # Gets the information about the file
    uploaded_file = request.FILES['file']
    file_size, file_name = uploaded_file.size, "ID" + str(File.objects.all().count()) + "%" + request.POST['name']

    # Stops the processing if the file size is too large
    if(file_size > 5242880):
      return render_to_response('upload_form.html', { 'large_file' : '1' }, context_instance=RequestContext(request))

    file_path, file_course = '/home/simon/Centum/static/notes/' + file_name, request.POST['course']

    # Reads and writes the chunks
    with open(file_path, 'wb+') as destination:
      for chunk in request.FILES['file'].chunks():
        destination.write(chunk)     

    # Saves the file
    upload = File(
      name = file_name,
      real_name = file_name[file_name.find('%') + 1:],
      user = request.user,
      path = file_path,
      size = int(file_size),
      course = file_course.upper(),
      date_uploaded = datetime.datetime.now()
    )
    upload.save()
    return render_to_response('upload_form.html', {'success' : '1'}, context_instance=RequestContext(request))  

要使其在上载时保持为.docx格式,请执行以下操作?

docx文件是zip文件。django是否使用zip扩展名保存它并不重要,因为zip扩展名不会改变内容,您可以使用任何您喜欢的文件名提供它。你面临的实际问题是什么

这并不出乎意料,因为docx/xlsx/。。。文件(及其LibreOffice对应文件)只是包含(大部分)XML数据的zip文件。有没有办法解决这个问题?