欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

flask实现上传

程序员文章站 2022-07-12 17:26:56
...
UPLOAD_FOLDER = 'ipa/'
ALLOWED_EXTENSIONS = set(['ipa'])
ALLOWED_TYPE = set(['online', 'offline', 'release'])

app = Flask(__name__)

def allowed_file(filename):
    return '.' in filename and \
        filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

def allowed_type(typename):
    return typename in ALLOWED_TYPE

@app.route('/upload/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST' and request.form['type'] and allowed_type(request.form['type']):
        file = request.files['file']
        filename = request.form['rename'] if 'rename' in request.form else file.filename
	print filename
        if file and allowed_file(filename):
            filename = secure_filename(filename)
            file.save(os.path.join(UPLOAD_FOLDER, request.form['type'], filename))
            return 'success\n'
    return 'failed\n'

if __name__ == '__main__':
    app.debug = True
    app.run(host = '0.0.0.0', port = 8012)



对应的上传命令
curl -i -F "file=@${foo}/${bar}" -F "type=online" http://${ip}:${port}/upload/
curl -i -F "file=@${foo}/${bar}" -F "rename=${rename}" -F "type=online" http://${ip}:${port}/upload/