1

所以我使用标准的上传文件代码在我的网站上制作了上传功能。我测试过的所有文件类型(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?

4

1 回答 1

0

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

于 2013-10-28T19:19:34.060 回答