โดย bom_002 » 20/03/2017 3:19 pm
File GET Data File upload.py
โค้ด: เลือกทั้งหมด
data = 'name='+image_filename
request = Request.from_values(query_string='foo=bar&blah=blafasel',content_length = len(data), input_stream = StringIO(data),
content_type = 'http://localhost:5002/',method = 'POST')
File Show GET Data File view.py
โค้ด: เลือกทั้งหมด
UPLOAD_FOLDER = '/home/com003/PycharmProjects/test_project/work01/'
def view_file(req, file_img=None):
print UPLOAD_FOLDER
#print req.method
if request.method == 'POST':
file_img = request.form['name']
print 'user = '+file_img
return file_img
file = UPLOAD_FOLDER +file_img
print 'file = '+file
f = open(file,'r')
return BaseResponse(wrap_file(req.environ, f), mimetype='image/x-png',
direct_passthrough=True) #wrap_file(req.environ, f) ฟังก์ชันตัวมันเองไว้จัดการไฟล์ที่ upload เข้ามา โดยรับ paramiter จาก f = req.files['uploaded_file']
def application(environ, start_response):
req = BaseRequest(environ)
#if req.method == 'POST':
resp = view_file(req)
return resp(environ, start_response)
if __name__ == '__main__':
run_simple('localhost', 5002, application, use_debugger=True)
ผลการรันหน้าเว็บ
- 444.png (4.57 KiB) Viewed 850 times
ผลการรันหน้า view.py เกิด error
- 555.png (21.55 KiB) Viewed 850 times
[u]File GET Data [/u] File upload.py
[code]
data = 'name='+image_filename
request = Request.from_values(query_string='foo=bar&blah=blafasel',content_length = len(data), input_stream = StringIO(data),
content_type = 'http://localhost:5002/',method = 'POST')
[/code]
[u]File Show GET Data [/u] File view.py
[code]
UPLOAD_FOLDER = '/home/com003/PycharmProjects/test_project/work01/'
def view_file(req, file_img=None):
print UPLOAD_FOLDER
#print req.method
if request.method == 'POST':
file_img = request.form['name']
print 'user = '+file_img
return file_img
file = UPLOAD_FOLDER +file_img
print 'file = '+file
f = open(file,'r')
return BaseResponse(wrap_file(req.environ, f), mimetype='image/x-png',
direct_passthrough=True) #wrap_file(req.environ, f) ฟังก์ชันตัวมันเองไว้จัดการไฟล์ที่ upload เข้ามา โดยรับ paramiter จาก f = req.files['uploaded_file']
def application(environ, start_response):
req = BaseRequest(environ)
#if req.method == 'POST':
resp = view_file(req)
return resp(environ, start_response)
if __name__ == '__main__':
run_simple('localhost', 5002, application, use_debugger=True)
[/code]
ผลการรันหน้าเว็บ
[attachment=1]444.png[/attachment]
ผลการรันหน้า view.py เกิด error
[attachment=0]555.png[/attachment]