目的:从模具中接收文件,文件列表,并在任务中处理它们。但是,解决这个问题,一个问题,一个文件描述符在传输到任务时是关闭的。是这么想的还是有问题?我不想通过使用异步 aiohttp 的 javascript 文件来处理双引导。
服务器端切割
@app.post('/files', name="files_post")
async def files_post(request):
# read data file
data = await request.post()
print(data)
# filename for save
filename = data['filename'].filename
# get fd
iofile = data['filename'].file
path = app.static_path + '/{}'.format(filename)
# check work task --> fucken problem?>
app.loop.create_task(app.savefile(filename=filename,iofile=iofile, data=data))
return web.Response(body=b"ffs suxx upload..")
with open(path, "wb") as fout:
while iofile.closed:
buf = iofile.read(buffer_size=1024)
fout.write(buf)
print("File {} success upload".format(filename))
return web.Response(body=content,
headers=MultiDict(
{'Content-Disposition: attachment; filename="{}"'.format(filename): filename}))
html侧切
<strong>Upload</strong>
<form method="post" action="/files" enctype="multipart/form-data">
<strong>Choise files: </strong> <input type="file" name="filename" multiple>
<input type="submit" name="sender" value="Send" />
</form>
任务切割
async def savefile(self, filename, iofile, ptosave=None, data=None):
if isinstance(iofile, io.BufferedRandom):
path = os.path.join((ptosave or self.static_path_upload), filename)
fin = fout = None
print('[TASK] From task:', iofile)
print("[TASK] -->", data['filename'].file.closed)
print("[TASK] --> File upload closed: ", iofile.closed)
try:
iofile.readable().type(iofile)
except ValueError as err:
print("[TASK] readable: {}".format(err))
return
# back
if not iofile.closed:
try:
with open(path,"wb") as fout:
buf=iofile.read(1024) # read 1024 bytes
fout.write(buf) # write buf ~ len(buf) bytes
except IOError as err:
print("SAVENAME: ", err)
finally:
if fout:
fout.close()
print("File {} upload".format(filename))
调试消息剪切
[MIDDLEWARE - database] [ Engine creating ] <aiomysql.sa.engine.Engine object at 0x8084790b8>
Method: POST
Path: /files
Cookies: {}
[MIDDLEWARE - database] [ Engine creating ] <aiomysql.sa.engine.Engine object at 0x80848ea90>
<MultiDictProxy('filename': Field(name='filename', filename='8.png', file=<_io.BufferedRandom name=10>, content_type='image/png'), 'sender': 'Send')>
[TASK] From task: <_io.BufferedRandom name=10>
[TASK] --> True
[TASK] --> File upload closed: True
[TASK] readable: I/O operation on closed file