Я ищу способ загрузить файл с помощью Powershell в API, написанный на Python.
На стороне сервера: импорт ОС из flask импорт Flask, jsonify, запрос, отмена, fla sh, перенаправление, url_for из werkzeug.utils import secure_filename
UPLOAD_FOLDER = 'd:/Temp/11aa/'
ALLOWED_EXTENSIONS = {'jpg'}
app = Flask(__name__)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/attachmnt', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
if __name__ == '__main__':
app.secret_key = 'pwd'
app.run(debug=True)
Запрос от Powershell:
Invoke-WebRequest -uri 'http://127.0.0.1:5000/attachmnt' -Method Post -Infile "./test.jpg" -ContentType 'image/jpg' -UseDefaultCredentials