django将文件从服务器下载到用户';s机器,或在线阅读
我正在服务器上上传一些.doc和.txt文档,我希望有两个选项: -用户必须能够下载文档 -用户必须能够在线阅读 我已经阅读了一些下载功能的代码,但它似乎不起作用 我的代码:django将文件从服务器下载到用户';s机器,或在线阅读,django,forms,download,Django,Forms,Download,我正在服务器上上传一些.doc和.txt文档,我希望有两个选项: -用户必须能够下载文档 -用户必须能够在线阅读 我已经阅读了一些下载功能的代码,但它似乎不起作用 我的代码: def download_course(request, id): course = Courses.objects.get(pk = id) response = HttpResponse(mimetype='application/force-download') response['Conte
def download_course(request, id):
course = Courses.objects.get(pk = id)
response = HttpResponse(mimetype='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(/root/)
return response
def save_course(request, classname):
classroom = Classroom.objects.get(classname = classname)
if request.method == 'POST':
form = CoursesForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['course'])
new_obj = form.save(commit=False)
new_obj.creator = request.user
new_obj.classroom = classroom
new_obj.save()
return HttpResponseRedirect('.')
else:
form = CoursesForm()
return render_to_response('courses/new_course.html', {
'form': form,
},
context_instance=RequestContext(request))
def handle_uploaded_file(f):
destination = open('root', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
有线索吗?
谢谢 响应['X-Sendfile']是否应该指向该文件?看起来它只是指向“/root/”,我猜它只是一个目录。也许它应该看起来更像这样:
def download_course(request, id):
course = Courses.objects.get(pk = id)
path_to_file = get_path_to_course_download(course)
response = HttpResponse(mimetype='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(path_to_file)
return response
Where
get\u path\u to\u course\u download
返回下载在文件系统中的位置(例如:/path/to/Where/handle\u upload\u files/saves/files/the\u file.doc响应['X-Sendfile']是否指向该文件?看起来它只是指向“/root/”,我猜它只是一个目录。也许它应该看起来更像这样:
def download_course(request, id):
course = Courses.objects.get(pk = id)
path_to_file = get_path_to_course_download(course)
response = HttpResponse(mimetype='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(path_to_file)
return response
其中get\u path\u to\u course\u download
返回下载在文件系统中的位置(例如:/path/to/Where/handle\u uploaded\u files/saves/files/the\u file.doc您可以打开一个文件对象来读取实际文件,然后像下面的代码一样开始下载文件:
path_to_file = os.path.realpath("random.xls")
f = open(path_to_file, 'r')
myfile = File(f)
response = HttpResponse(myfile, content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=' + name
return response
指向文件的路径
:是文件在服务器上的位置。
f=open(指向文件“r”的路径)
。。读取文件
剩下的就是下载文件。您可以打开一个文件对象来读取实际文件,然后开始下载文件,如下代码所示:
path_to_file = os.path.realpath("random.xls")
f = open(path_to_file, 'r')
myfile = File(f)
response = HttpResponse(myfile, content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=' + name
return response
指向文件的路径
:是文件在服务器上的位置。
f=open(指向文件“r”的路径)
。。读取文件
剩下的就是下载文件。您需要在某个地方定义文件名!它是否存储在
Courses`模型中?(我想应该是Courses.course)?您需要在某个地方定义文件名!它是否存储在
Courses`模型中?(我想它应该是Courses.course)?类是来自django.core.files导入文件的,类是来自django.core.files导入文件的,
“获得意外参数mimetype”“获得意外参数mimetype”