1.讓別人下載已存在的檔案
import os
from flask import Flask, send_from_directory
app = Flask(__name__)
@app.route("/download/<filename>")
def download(filename):
dirpath = os.path.join(app.root_path, 'download')
return send_from_directory(dirpath, filename, as_attachment=True)
2.讓別人上傳檔案
[參考頁面](http://blog.csdn.net/qq_25730711/article/details/53643758)
3.讓別人下載頁面上的資料
from flask_restful import Api, Resource
restApi = Api(app,prefix='/api')
class downloadApi(Resource):
def get(self):
data = {'test':1}
return Response(
json.dumps(data),
mimetype='application/json',
headers={'Content-disposition':
'attachment; filename={0}.json'.format(datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))})
restApi.add_resource(downloadApi,'/download')
class downloadApi(Resource):
def get(self):
csv = '1,2,3\n4,5,6\n'
return Response(
csv,
mimetype='text/csv',
headers={'Content-disposition':
'attachment; filename={0}.csv'.format(datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))})
restApi.add_resource(downloadApi,'/download')
[flask_csv官方文件](https://pypi.python.org/pypi/Flask-CSV/1.0.1)
from flask_csv import send_csv
class downloadApi(Resource):
def get(self):
return send_csv([{'name':'josh'},{'name':'tony'}],
"{0}.csv".format(datetime.now().strftime('%Y-%m-%d_%H-%M-%S')),
['name'])
restApi.add_resource(downloadApi,'/download')