Javascript 文件上载后使用render推送flask调用时页面未呈现?
问题: Iam有一个文件上传页面(下面的代码),其中有一个选项可以上传文件,上传时会触发post请求Javascript 文件上载后使用render推送flask调用时页面未呈现?,javascript,jquery,python,python-2.7,flask,Javascript,Jquery,Python,Python 2.7,Flask,问题: Iam有一个文件上传页面(下面的代码),其中有一个选项可以上传文件,上传时会触发post请求/uploadFiles1,由flask处理(下面的代码)在此之后,它会触发文件上的一系列事件,并且在所有活动完成后,我希望在同一页面中显示一条消息Success,但它不会显示 flask_app.py @app.route("/uploadFiles") def index(): return render_template('upload.htm') , 1 @app.rout
/uploadFiles1
,由flask处理(下面的代码)在此之后,它会触发文件上的一系列事件,并且在所有活动完成后,我希望在同一页面中显示一条消息Success
,但它不会显示
flask_app.py
@app.route("/uploadFiles")
def index():
return render_template('upload.htm') , 1
@app.route('/uploadFiles1', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
if f:
filename = secure_filename(f.filename)
f.save( os.path.join(app.config['UPLOAD_FOLDER'], filename ))
copyfile(os.path.join(app.config['UPLOAD_FOLDER'],filename) ,os.path.join('/home/admin/Batch/Samples/', filename))
# Call the remote function
from paramiko import SSHClient
client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect("192.168.90.90", username="nsadmin",password='something')
transport = client.get_transport()
#stdin , stdout , stderr =client.exec_command('sudo ls')
stdin, stdout, stderr = client.exec_command('sudo python /home/nsadmin/app.py')
print "stderr: ", stderr.readlines()
print "Output: ", stdout.readlines()
# Remove this file from /Batch/Samples/ Location
#os.remove(os.path.join('/home/admin/Batch/Samples/', filename))
print 'I came here'
msg = 'File upload Success'
logger.info('I came here with msg {}'.format(msg))
return render_template('upload.htm', msg=msg) , 1
upload.htm
<!DOCTYPE html>
<html>
<head>
<title>File Upload </title>
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel='stylesheet' type='text/css' />
<!-- Custom Theme files -->
<link href="{{ url_for('static', filename='css/style.css') }}" rel="stylesheet" type="text/css" media="all" />
<!-- Custom Theme files -->
<script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
<!-- Custom Theme files -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
</head>
<body>
<h1>Upload Malware Here</h1>
<h3>
{% if msg %}
{{ msg }} #empty page
{% endif %}
</h3>
<div class="upload">
<h3>Select File</h3>
<div class="login-form">
<form id="upload" method="POST" action="/uploadFiles1" enctype="multipart/form-data">
<div id="drop">
<a>Upload</a>
<input type="file" name="file" />
</div>
<input type="submit" value="submit">
<ul>
<!-- The file uploads will be shown here -->
</ul>
</form>
</div>
</div>
<!-- JavaScript Includes -->
<script src="{{ url_for('static', filename='js/jquery.knob.js') }}"></script>
<!-- JavaScript Includes -->
<!-- jQuery File Upload Dependencies -->
<script src="{{ url_for('static', filename='js/jquery.ui.widget.js') }}"></script>
<script src="{{ url_for('static', filename='js/jquery.iframe-transport.js') }}"></script>
<script src="{{ url_for('static', filename='js/jquery.fileupload.js') }}"></script>
<!-- jQuery File Upload Dependencies -->
<!-- Main JavaScript file -->
<script src="{{ url_for('static', filename='js/script.js') }}"></script>
<!-- Main JavaScript file -- >
</body>
</html>
文件上传
addEventListener(“加载”,函数(){setTimeout(hideURLbar,0);},false);函数hideURLbar(){window.scrollTo(0,1);}
在这里上传恶意软件
{%if msg%}
{{msg}}#空页
{%endif%}
选择文件
更新:
响应正在填充,但页面未使用元素更新
看起来您是在使用Javascript通过XHR上传文件,而不是使用浏览器执行完整的POST请求:
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();
这意味着呈现的响应将被发送回Javascript,而Javascript会忽略它
您有两个选择:
通过禁用Javascript上传并让用户提交完整表单来发布标准帖子,在这种情况下,您的消息应该正确呈现
检查Javascript是否成功并呈现消息本身
首先,我将使用Chrome的开发工具查看呈现成功页面的来源。查看
块发生了什么,您的消息应该放在哪里。将完整的输出放在a中也有助于得到答案。
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();