Skip to content

Instantly share code, notes, and snippets.

@vulcan25
Last active December 28, 2020 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vulcan25/8e9444750426cdc7eb2cfd0a0f50a05c to your computer and use it in GitHub Desktop.
Save vulcan25/8e9444750426cdc7eb2cfd0a0f50a05c to your computer and use it in GitHub Desktop.
Flask upload with dropzone.

Be warned understand this script before you launch. Anywhere this runs, files can be uploaded. No auth or limiting is built in. Useful for personal use.

Set the config vars at the top of app.py

Template loads dropzone from cdnjs.cloudflare.com.

Install

python3 -m venv venv
source venv/bin/activate
pip install flask
python3 app.py

Usage

Load http://localhost:5000/ in your browser and use the dropzone GUI.

From any terminal:

touch upload_me.txt  && echo I am the contents > upload_me.txt
curl -i -X POST -F 'file=@upload_me.txt' "http://localhost:5000/upload" -H 'ContentType: multipart/form-data'
from flask import Flask, render_template, request
import werkzeug, os
from werkzeug.utils import secure_filename
app = Flask(__name__, template_folder='.')
# Configurationg
UPLOAD_FOLDER = '/tmp'
ALLOWED_EXTENSIONS = ['csv','txt']
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['POST'])
def upload():
data = request.files
if data['file'] == None:
return "no file", 400
file = data['file']
if file and allowed_file(file.filename):
file.save(os.path.join(UPLOAD_FOLDER, secure_filename(file.filename)))
return "file uploaded", 202
else:
return 'not allowed', 403
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
from python:alpine
RUN pip install -U pip && pip install flask
WORKDIR /code
COPY ./app.py /code
COPY ./index.html /code
CMD flask run -h 0.0.0.0
<html>
<head>
<title> Dropzone</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.0/dropzone.css" integrity="sha256-0Z6mOrdLEtgqvj7tidYQnCYWG3G2GAIpatAWKhDx+VM=" crossorigin="anonymous" />
</head>
<body>
<form method='POST'
enctype="multipart/form-data"
class="dropzone"
id="my-awesome-dropzone">
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.0/dropzone.js" integrity="sha256-NLit4Z57jz8npRHkopjfr68hSZY1x/ruN0T8kf68xq4=" crossorigin="anonymous"></script>
<script type='text/javascript'>
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("form#my-awesome-dropzone",
{ url: "/upload",
});
</script>
</body>
</html>
@vulcan25
Copy link
Author

Usage:

python3 -m venv venv
source venv/bin/activate
pip install flask flask-restful
python3 app.py

In another terminal:

touch upload_me.txt  && echo I am the contents > upload_me.txt
curl -i -X POST -F 'file=@upload_me.txt' "http://localhost:5000/upload" -H 'ContentType: multipart/form-data'

@vulcan25
Copy link
Author

vulcan25 commented Aug 19, 2019

For powershell use something like this (credit):

echo I am the contents > test.txt

$FilePath = '.\test.txt';

$filename = (Get-Item $file).Name
$URL = 'http://localhost:5000/upload';

$fileBytes = [System.IO.File]::ReadAllBytes($FilePath);
$fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString(); 
$LF = "`r`n";

$bodyLines = ( 
    "--$boundary",
    "Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"",
    "Content-Type: multipart/form-data$LF",
    $fileEnc,
    "--$boundary--$LF" 
) -join $LF

Invoke-RestMethod -Uri $URL -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines

@vulcan25
Copy link
Author

vulcan25 commented Jan 4, 2020

Python method:

import requests

def upload(filename):
	with open(filename,'rb') as f:
		files = {'file': f}
		url='http://localhost:5000/upload'
		r = requests.post(url, files=files)
		print (r.content, r.status_code)

upload('image.jpg')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment