โดย bom_002 » 16/03/2017 4:28 pm
โค้ดทั้งหมดครับ
โค้ด: เลือกทั้งหมด
#!/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
from werkzeug.wsgi import wrap_file
import os
import PythonMagick
from unipath import Path
import shutil
def view_file(req):
if not 'uploaded_file' in req.files:
return BaseResponse('no file uploaded')
f = req.files['uploaded_file']
j = os.path.realpath('uploaded_file')
shutil.copyfile(j,"Documents",f)
print f
print j
return BaseResponse(wrap_file(req.environ, f), mimetype=f.content_type,
direct_passthrough=True) #wrap_file(req.environ, f) ฟังก์ชันตัวมันเองไว้จัดการไฟล์ที่ upload เข้ามา โดยรับ paramiter จาก f = req.files['uploaded_file']
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)
ส่วนนี้ที่แก้ไขครับ
ยังติดปัญหาหลังจากการอัพไฟล์แล้ว จะก๊อบปี้ไฟล์ลงเครื่องครับ ลองใช้ import shutil แต่ยังไม่ได้ครับ
โค้ด: เลือกทั้งหมด
def view_file(req):
if not 'uploaded_file' in req.files:
return BaseResponse('no file uploaded')
f = req.files['uploaded_file']
j = os.path.realpath('uploaded_file')
shutil.copyfile(j,"Documents",f)
print f
print j
return BaseResponse(wrap_file(req.environ, f), mimetype=f.content_type,
direct_passthrough=True) #wrap_file(req.environ, f) ฟังก์ชันตัวมันเองไว้จัดการไฟล์ที่ upload เข้ามา โดยรับ paramiter จาก f = req.files['uploaded_file']
อันนี้ error จากฝั่ง server
- 44554.png (30.6 KiB) Viewed 2286 times
อันนี้ error จากโปรแกรมครับ
- 4445.png (84.55 KiB) Viewed 2286 times
[u][b]โค้ดทั้งหมดครับ[/b][/u]
[code]
#!/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
from werkzeug.wsgi import wrap_file
import os
import PythonMagick
from unipath import Path
import shutil
def view_file(req):
if not 'uploaded_file' in req.files:
return BaseResponse('no file uploaded')
f = req.files['uploaded_file']
j = os.path.realpath('uploaded_file')
shutil.copyfile(j,"Documents",f)
print f
print j
return BaseResponse(wrap_file(req.environ, f), mimetype=f.content_type,
direct_passthrough=True) #wrap_file(req.environ, f) ฟังก์ชันตัวมันเองไว้จัดการไฟล์ที่ upload เข้ามา โดยรับ paramiter จาก f = req.files['uploaded_file']
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)
[/code]
[u][b]ส่วนนี้ที่แก้ไขครับ[/b][/u]
ยังติดปัญหาหลังจากการอัพไฟล์แล้ว จะก๊อบปี้ไฟล์ลงเครื่องครับ ลองใช้ import shutil แต่ยังไม่ได้ครับ
[code]
def view_file(req):
if not 'uploaded_file' in req.files:
return BaseResponse('no file uploaded')
f = req.files['uploaded_file']
j = os.path.realpath('uploaded_file')
shutil.copyfile(j,"Documents",f)
print f
print j
return BaseResponse(wrap_file(req.environ, f), mimetype=f.content_type,
direct_passthrough=True) #wrap_file(req.environ, f) ฟังก์ชันตัวมันเองไว้จัดการไฟล์ที่ upload เข้ามา โดยรับ paramiter จาก f = req.files['uploaded_file']
[/code]
อันนี้ error จากฝั่ง server
[attachment=1]4445.png[/attachment]
อันนี้ error จากโปรแกรมครับ
[attachment=0]44554.png[/attachment]