-
Notifications
You must be signed in to change notification settings - Fork 64
/
上传文件脚本.py
71 lines (65 loc) · 2.01 KB
/
上传文件脚本.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# coding=utf8
import base64
from flask import Flask, request
app = Flask(__name__)
file_html = r'''
<html>
<head>
<script type="text/javascript">
function formSubmit() {
function cback(){
try{
document.getElementById("decode").value = xml.response;
}catch{
document.getElementById("decode").value = '正在传输数据,请等待';
}
}
function POST(url, headers, file){
var xml = new XMLHttpRequest();
xml.onreadystatechange = cback;
xml.open('POST', url, true);
Object.keys(headers).map((eachKey)=>{ xml.setRequestHeader(eachKey, headers[eachKey]); })
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e){
xml.send(e.target.result);
};
return xml;
}
var info = document.getElementById("file_test_interface").files[0]
var v = document.getElementById("file_test_interface").value;
var href = '/file_test_interface?filename=' + v.substring(v.lastIndexOf("\\")+1);
var xml = POST(href, {}, info)
}
</script>
</head>
<body>
<form enctype='multipart/form-data' method='POST'>
<input type="file" name="file" id="file_test_interface" />
</form>
<br />
<button onclick="formSubmit()">提交文件</button>
<br />
<textarea id="decode" size="20" style="width:1500px;height:300px"></textarea>
</body>
</html>
'''
@app.route('/')
def index():
return file_html
@app.route('/file_test_interface', methods=['POST'])
def file_test_interface():
try:
filename = request.args.get('filename')
# print(request.get_data().decode().split('base64,'))
file = request.get_data().decode().split('base64,')[-1]
filedata = base64.b64decode(file)
# 这里返回的是字符串,通常用于解析二进制内容后返回字符串类型数据
with open(filename, 'wb') as f:
f.write(filedata)
return '传输数据成功.'
except:
import traceback; traceback.print_exc()
return "传输数据失败."
if __name__ == '__main__':
app.run("0.0.0.0", port=12333)