views.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:MusicQR 作者: tangziyi001 项目源码 文件源码
def download(request, token):
    context = {}
    if request.method == 'GET':
        try:
            # check token in database and retrieve corresponding music
            targetQuery = MusicQuery.objects.get(token=token)
            targetMusic = targetQuery.query
            file_name = targetMusic.file_name
            dir_path = os.path.dirname(os.path.realpath(__file__))
            file_path = dir_path + '/music/' + file_name
            file_wrapper = FileWrapper(file(file_path, 'rb'))
            file_mimetype = mimetypes.guess_type(file_path)

            # store the download record
            newDownload = Download.objects.create(music=targetMusic, download_loc='')
            newDownload.save()

            # download the music file
            response = HttpResponse(file_wrapper, content_type=file_mimetype)
            response['X-Sendfile'] = file_path
            response['Content-Length'] = os.stat(file_path).st_size
            response['Content-Disposition'] = 'attachment; filename=' + file_name
            targetQuery.delete()
            return response
        except Exception as e:
            messages.add_message(request, messages.ERROR, "Music Download Failed: QR Code Expired")
            logging.exception("message")
            return redirect('/musician')
    else:
        return redirect('/musician')

# artist upload handler
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号