मैं application/octet-stream
प्रकार पोस्ट का उपयोग कर काम करने का अनुरोध प्राप्त करने में असमर्थ था, लेकिन फ्लास्क का उपयोग कर छवियों को अपलोड करने के लिए अतीत में multipart/form-data
प्रकारों का उपयोग किया है।
मैं विस्तार किया है कि मैं क्या अनेक अपलोड फ़ाइलों का समर्थन करने के लिए अतीत में किया है और इस WERKZEUG के FileStorage
वस्तुओं का लाभ काम किया है।
यहां कुंजी एक पोस्ट आधारित मार्ग स्थापित कर रही है जो किसी फ़ॉर्म से अनुरोध तत्व की तलाश में है। यह आपको किसी मानक रूप या AJAX कॉल के माध्यम से मार्ग पर पोस्ट करने की अनुमति दे सकता है।
देखने के लिए खाका:
नीचे एक सरलीकृत उदाहरण है एक फार्म का उपयोग कर रहा है
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>jQuery File Upload Example</title>
</head>
<body>
{% if err %}
<h4>{{ err }}</h4>
{% endif %}
<form action="/" method=POST enctype=multipart/form-data id="fileupload">
<input type="file" name="files" data-url="/" multiple>
<input type=submit value=Post>
</form>
{% if files %}
{% for file in files %}
<p>Uploaded: <b>{{ file }}</b> </p>
{% endfor %}
{% endif %}
</body>
</html>
बोतल अनुप्रयोग
from flask import Flask, request, render_template
from werkzeug import secure_filename, FileStorage
import os
# Flask functions
app = Flask(__name__)
app.config.from_object(__name__)
DEBUG = True
# add this so that flask doesn't swallow error messages
app.config['PROPAGATE_EXCEPTIONS'] = True
@app.route('/', methods=['GET', 'POST'])
def uploader():
if request.method =='POST' and request.files.getlist('files'):
up_file_list = []
# Iterate the through a list of files from the form input field
for a_file in request.files.getlist('files'):
if a_file.filename:
# Validate that what we have been supplied with is infact a file
if not isinstance(a_file, FileStorage):
raise TypeError("storage must be a werkzeug.FileStorage")
# Sanitise the filename
a_file_name = secure_filename(a_file.filename)
# Build target
a_file_target = os.path.join('/tmp/', a_file_name)
# Save file
a_file.save(a_file_target)
up_file_list.append(a_file_name)
# Return template
if up_file_list:
return render_template('uploader.html', err=None, files=up_file_list)
else:
return render_template('uploader.html', err='No Files Uploaded', files=None)
else:
return render_template('uploader.html', err=None, files=None)
# application execution
if __name__ == '__main__':
app.run()
आप Chrome पर हैं, तो डेवलपर टूल के नेटवर्क टैब को देखें और देखें कि दो अनुरोध कैसा दिखते हैं। – Blender
------ WebKitFormBoundaryxInpKfVFK8mUuqOv सामग्री-विस्थापन: फ़ॉर्म-डेटा; नाम = "post_photo_gallery"; filename = "" सामग्री-प्रकार: अनुप्रयोग/ऑक्टेट-स्ट्रीम लाइन AJAX अनुरोध – saidozcan
पर दिखाई नहीं दे रही हैं यह उपयोगी हो सकती है: https://github.com/blueimp/jQuery-File-Upload/wiki/Flask – Blender