我有一个文件扩展名为 log 的文件。
我想在新窗口中(在 Chrome 中)显示此日志文件的内容。但是,当我像这样调用我的 window.open 函数时:
window.open('/log/eppsta.log ', "", "height=800,width=1200,toolbar=0,menubar=0");
我得到了新窗口,但文件是作为附件下载的,我的窗口如下所示:
如何在新浏览器窗口中显示日志文件内容(作为普通文本)?
最佳答案
假设您不想更改文件的 HTTP header ,您可以使用 XHR 来获取日志。或fetch API ,然后在新窗口上使用document.write将日志写入窗口:
fetch('/log/eppsta.log')
.then(res => res.text())
.then(text =>
window.open('', '', 'height=800,width=1200,toolbar=0,menubar=0')
.document.write(text)
)
.catch(err => throw err) // Handle any errors
如果你想让它以等宽的方式打印出来,就像日志一样,也许可以将 body
包裹起来。在 document.write 中使用 <pre>
进行调用像这样的标签:.document.write('<pre>' + body + '</pre>')
关于javascript - window.open 在窗口中显示日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44203875/