Bootstrap

python的django框架下载_Django框架文件下载的实现?

用 Django 实现的资源分享网站,在写下载的时候出现了点问题,不知道怎么办,百度不到,就来问问,在线等,急。

这个是报的错,文件是通过 model 的 FileField 和 form 的 FileField 的传到 upload 文件夹的。

def resource_upload(req):

if req.method == 'POST':

ruf = ReUpForm(req.POST, req.FILES)

if ruf.is_valid():

resource = Resource()

resource.re_name = ruf.cleaned_data['re_name']

resource.re_file = ruf.cleaned_data['re_file']

resource.post_img = ruf.cleaned_data['post_img']

resource.pre_img1 = ruf.cleaned_data['pre_img1']

resource.pre_img2 = ruf.cleaned_data['pre_img2']

resource.rating = ruf.cleaned_data['rating']

resource.re_class = ruf.cleaned_data['re_class']

resource.re_year = ruf.cleaned_data['re_year']

resource.summary = ruf.cleaned_data['summary']

resource.save()

return render_to_response('uploadsuccess.html', {})

else:

return render_to_response('upload.html', {'ruf': ruf, 'error_info': '上传出错,请重新填写上传信息.'})

else:

ruf = ReUpForm()

return render_to_response('upload.html', {'ruf': ruf, 'error_info': ''})

这是上传代码

def download(req):

re_id = int(req.session.get('id'))

resource = Resource.objects.get(id = re_id)

def file_iterator(file_name, chunk_size = 512):

with open(file_name, 'rb') as f:

while True:

c = f.read(chunk_size)

if c:

yield c

else:

break

f.close()

the_file_name = str(resource.re_file)

print type(the_file_name)

response = StreamingHttpResponse(file_iterator(the_file_name))

response['Content-Type'] = 'application/octet-stream'

response['Content-Disposition'] = 'attachment;filename="{0}"'.format(the_file_name)

return response

这是下载代码,麻烦帮我看下吧~~

;