如何使用ajax/php转发scp进度条?
我将
- 启动下载 (scp [email protected]/tmp/file /backup/) 从 Web 浏览器
- ajax (可能是 jQuery) 向 Web 服务器 (suse) 发出请求
- 其中 php 将运行 scp 命令
用户是否可以在浏览器中看到 scp 下载进度条?
I am going to
- initiate download (scp [email protected]/tmp/file /backup/) from a web browser
- ajax (could be jQuery) the request to web server (suse)
- where php will run the scp command
Is there any way that user could see the scp download progress bar in the browser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并非没有很多混乱。
在我的脑海中,您可以将 SCP 的输出重定向到一个文件,然后向解析该文件的脚本发出 AJAX 请求,当出现新行时(假设 scp 通过用 \r 分隔行来增加其进度条) PHP 脚本以某种方式退出,指示浏览器增加其进度条,然后再次启动 AJAX 进程......依此类推,直到 scp 终止。
我以前做过这样的事情,我想说的是,为了这么小的细节而付出巨大的努力是不值得的 - 除非你觉得你已经没有足够的事情要做......
做这样的事情会容易得多如果浏览器允许您在
ajax.readyState == 4
之前读取ajax.responseText
,但他们不这样做,那么事实并非如此。编辑
只是想到了一种替代方法,该方法在某些方面可能更容易,但在其他方面更困难:
如果您将进度条放在
中,您将不会不需要进行大量的 AJAX 调用,您可以通过服务器推送 javascript 来完成这一切 - 但您需要一个支持此功能的 Web 服务器,并且在某种程度上依赖于能够合理处理设置的浏览器。
Not without a lot of messing around.
Of the top of my head you could redirect the output of SCP to a file, then have an AJAX request to a script that parses that file, and when a new line appears (assuming scp increments it's progress bar by seperating lines with \r) the PHP script exits in some way that instructs the browser to increment it's progress bar, then initiate the AJAX process again... and so on until scp terminates.
I have done stuff like this before, and I would say that it's not worth the massive effort for such a small nicety - unless you feel you don't have enough to do already...
It would be much easier to do stuff like this if browsers would let you read
ajax.responseText
beforeajax.readyState == 4
, but they don't, so it isn't.EDIT
Just thought of an alternative approach to this that might be easier in some ways but more difficult in others:
If you put the progress bar in an
<iframe>
you wouldn't need to have the huge number of AJAX calls, and you could do it all by server-pushing javascript - but you will need a web server that supports this, and are somewhat relying on a browser that will handle the setup sensibly.