djangoファイルダウンロード機能を実現

3328 ワード

       
url(r'^api/download_excel/$', APIs.DownloadScoreExcel),
 
  
 
  
# download score excel
def DownloadScoreExcel(aRequest):
    file_name = aRequest.GET['file_name']
    file = os.getcwd() + '/tAPP/tAPPFile/' + file_name #     
    from django.utils.encoding import smart_str
    # mimetype       content_type   django 1.7        
    response = HttpResponse(content_type='application/force-download')  # mimetype is replaced by content_type for django 1.7
    response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
    response['X-Sendfile'] = smart_str(file)
 
  
    response.write(open(file,'rb').read())
    return response
 
  
 flask        :
 
  
# return excel
@app.route("/download/", methods=['GET'])
def down_excel(id):    #id     
    path = os.getcwd() + "/download/" #         
    if os.path.isfile(path + id):
        return send_from_directory(path, id)
    else:
        return jsonify(status='       ', code=404)