在我的servlet中,我有一个文件logs.txt存在的路径,如下所示,
String loc = "/u/xyz/workspace/FirstServlet/WebContent/WEB-INF/logs.txt";
并将以下行添加到我的servlet并将响应重定向到jsp页面
String result = "<a href='"+loc+"' target='_blank'>Download result</a>";
一切正常但当我点击下载结果时会返回资源未找到异常,我想将logs.txt文件下载到我的客户端下载文件夹。
我的错误是什么?
答案 0 :(得分:0)
通过使用path
request.setAttribute(arg0, arg1)
从servlet传递到Java脚本来尝试此操作
<script type='text/javascript'>
function saveTextAsFile()
{
var textToWrite = "SOME TEXT YOU WANT TO WRITE"
var textFileAsBlob = new Blob([textToWrite], {type:'.txt'});
var fileNameToSaveAs = "my.txt";
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
if (window.webkitURL != null)
{
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else
{
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
}
</script>