ให้ส่งค่ารูปภาพไปแสดงผลอีก url แล้วโชว์รูปภาพ ติด error จากโค้ดครับ หน้า upload.py
โค้ด: เลือกทั้งหมด
#!/usr/bin/env python
#-*-coding:utf-8 -*-
"""
Simple Upload Application
~~~~~~~~~~~~~~~~~~~~~~~~~
All uploaded files are directly send back to the client.
:copyright: (c) 2009 by the Werkzeug Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from werkzeug.serving import run_simple
from werkzeug.wrappers import BaseRequest, BaseResponse , Response
from werkzeug.wsgi import wrap_file
import os
from wand.image import Image, Color
from flask import Flask, request, redirect, url_for
import base64
UPLOAD_FOLDER = '/openerp7/custom/addons/learning_test/file/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def view_file(req):
if not 'uploaded_file' in req.files:
return BaseResponse('no file uploaded')
f = req.files['uploaded_file']
# print vars(file)
# print f.filename
f.save(os.path.join(app.config['UPLOAD_FOLDER'], f.filename))
#แปลงPDF to PNG
def convert_pdf(filename, output_path, resolution=150):
""" Convert a PDF into images.
All the pages will give a single png file with format:
{pdf_filename}-{page_number}.png
The function removes the alpha channel from the image and
replace it with a white background.
"""
all_pages = Image(filename=filename, resolution=resolution)
# print all_pages
for i, page in enumerate(all_pages.sequence):
with Image(page) as img:
img.format = 'png'
img.background_color = Color('white')
img.alpha_channel = 'remove'
image_filename = os.path.splitext(os.path.basename(filename))[0]
image_filename = '{}{}.png'.format(image_filename, i + 1)
image_filename = os.path.join(output_path, image_filename)
img.save(filename=image_filename)
app.route('127.0.0.1:5002/img/', methods=['GET', 'POST'])
print image_filename
convert_pdf("file/"+f.filename, "image/")
# fee = os.listdir("image")
image_aa = ''
#header ='''<!DOCTYPE html><html><body><title>test_server</title><table border="1" style="border-collapse:collapse;" align="center">'''
#fotor =''' </table><body><html>'''
# print fee
# i = 0
# for fill in fee:
# image = open('/openerp7/custom/addons/learning_test/image/'+fill, 'rb') #openbinary file in read mode
# image_read = image.read()
# image_64_encode = base64.encodestring(image_read)
add = Flask(__name__)
# print fill <img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
image_img = add.route('http://127.0.0.1:5002')
#image_aa += '''<tr><td>
#<img src = "data:image/png;base64,''' + image_img + '''" width = "300" height = "500" >
#</tr></td>'''
#return BaseResponse(header+image_aa+fotor, mimetype='text/html')
return BaseResponse(image_img, mimetype='text/html')
def upload_file(req): #returnหน้า form เป็น code html ออกไป
return BaseResponse('''
<h1>Upload File</h1>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_file">
<input type="submit" value="Upload">
</form>
''', mimetype='text/html')
def application(environ, start_response):
req = BaseRequest(environ)
if req.method == 'POST':
resp = view_file(req)
else:
resp = upload_file(req)
return resp(environ, start_response)
if __name__ == '__main__':
run_simple('localhost',5004, application, use_debugger=True)
โค้ด: เลือกทั้งหมด
#!/usr/bin/env python
#-*-coding:utf-8 -*-
from werkzeug.serving import run_simple
from werkzeug.wrappers import BaseRequest, BaseResponse , Response
import os
from flask import Flask, request, redirect, url_for
import base64
app = Flask(__name__)
def view_file(req):
if not 'uploaded_file' in req.files:
return BaseResponse('no file uploaded')
app.route('/img/',methods = ['POST', 'GET'])
def img(imgs):
fee = os.listdir(imgs)
image_aa = ''
header ='''<!DOCTYPE html><html><body><title>test_server</title><table border="1" style="border-collapse:collapse;" align="center">'''
fotor =''' </table><body><html>'''
i = 0
for fill in fee:
image = open('/openerp7/custom/addons/learning_test/'+fill, 'rb') #openbinary file in read mode
image_read = image.read()
image_64_encode = base64.encodestring(image_read)
image_aa += '''<tr><td>
<img src = "data:image/png;base64,''' + image_64_encode + '''" width = "300" height = "500" >
</tr></td>'''
return BaseResponse(header+image_aa+fotor, mimetype='text/html')
def upload_file(req): #returnหน้า form เป็น code html ออกไป
return BaseResponse('''
<h1>Upload File</h1>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_file">
<input type="submit" value="Upload">
</form>
''', mimetype='text/html')
def application(environ, start_response):
req = BaseRequest(environ)
if req.method == 'POST':
resp = view_file(req)
else:
resp = upload_file(req)
return resp(environ, start_response)
if __name__ == '__main__':
run_simple('localhost',5002, application, use_debugger=True)
โค้ด: เลือกทั้งหมด
/usr/bin/python2.7 /openerp7/custom/addons/learning_test/upload_img.py
* Running on http://localhost:5004/ (Press CTRL+C to quit)
127.0.0.1 - - [28/Mar/2017 14:42:20] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [28/Mar/2017 14:42:20] "GET /favicon.ico HTTP/1.1" 200 -
image/python_1.png
image/python_2.png
image/python_3.png
image/python_4.png
Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 693, in __next__
return self._next()
File "/usr/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 81, in _iter_encoded
for item in iterable:
TypeError: 'function' object is not iterable
127.0.0.1 - - [28/Mar/2017 14:42:46] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [28/Mar/2017 14:42:46] "GET /favicon.ico HTTP/1.1" 200 -
Process finished with exit code 0
ว่างเปล่า