Django:文件下载后重定向 [英] Django: redirect after file download

查看:207
本文介绍了Django:文件下载后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Django 1.5 中的视图进行操作,该视图允许我下载文件。由 HTML 页面中的按钮触发的下载过程如下:

I'm working on a view in my Django 1.5 that allow me to download a file. The download process it's triggered by a button in the HTML page like this:

<a href="/file/download/{{ file.name }}/"><input type="button" value="Download!" /></a>

指向管理下载的视图的网址:

The url point to a view that manage the download:

def filedownload(request, filename):
    down_file = File.objects.get(name = filename)
    file_path = MEDIA_ROOT+str(down_file.file)
    file_name = down_file.filecomplete()
    if not Transaction.objects.filter(user = request.user, file = down_file):
        transaction = Transaction.objects.create(date = datetime.now(), user = request.user, file = down_file, vote = False)
        transaction.save()
    fp = open(file_path, 'rb')
    response = HttpResponse(fp.read())
    fp.close()
    type, encoding = mimetypes.guess_type(file_name)
    if type is None:
        type = 'application/octet-stream'
    response['Content-Type'] = type
    response['Content-Length'] = str(os.stat(file_path).st_size)
    if encoding is not None:
        response['Content-Encoding'] = encoding
    if u'WebKit' in request.META['HTTP_USER_AGENT']:
        filename_header = 'filename=%s' % file_name.encode('utf-8')
    elif u'MSIE' in request.META['HTTP_USER_AGENT']:
        filename_header = ''
    else:
        filename_header = 'filename*=UTF-8\'\'%s' % urllib.quote(file_name.encode('utf-8'))
    response['Content-Disposition'] = 'attachment; ' + filename_header
    return response

我想要做的是将用户重定向到成功页面后,他们按下了按钮,但我找不到解决方法。

What I wanted to do it's to redirect the user to a success page right after they hit the downlad button but I can't find a way to do it.

我不担心下载中断或下载失败,因为这是一个

I'm not concerned about interrupted or otherwise unsuccessful downloads since it's a school project.

推荐答案

他是运行代码所必须遵循的所有步骤:

He are all steps that you have to follow to run your code :

获取 jQuery文件下载,它允许使用OnSuccess和OnFailure回调进行下载。

get the jQuery File Download which allows downloads with OnSuccess and OnFailure callbacks.

这里是一个使用插件源的简单用例演示承诺。演示页面还包含许多其他更好的UX示例。

Here is a simple use case demo using the plugin source with promises. The demo page includes many other, 'better UX' examples as well.

$.fileDownload('some/file.pdf')
    .done(function () { //redirect });

这是一个使用插件并带有承诺。 演示页面还包括许多其他更好的用户体验示例。

Here is a simple use case demo using the plugin source with promises. The demo page includes many other, 'better UX' examples as well.

这篇关于Django:文件下载后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆