gpt4 book ai didi

javascript - 请求通过ajax下载servlet文件(请不要使用jquery..)

转载 作者:行者123 更新时间:2023-12-03 10:22:39 26 4
gpt4 key购买 nike

流程是这样的:

  1. 从网络(jsp)上传一些pdf文件(通过ajax提交)
  2. 在后端我合并这些pdf
  3. 我通过ajax得到响应(合并的pdf)-->开始文件下载...

我在第三步中遇到问题。

我仅包含了提交要上传的文件(发布请求)并开始下载的相关代码。我还放置了一个直接链接,它在 get 方法中调用相同的步骤并且有效。

我的问题出在哪里?提前致谢...

这是jsp主体标签

<a href="/TestAjaxServletDownload/DownloadServlet" >
download
</a>

<p><input id="sampleFile5" name="sampleFile5" type="file" /></p>

<p><input id="uploadBtn" type="button" value="Upload" onClick="javascript:performAjaxSubmit();"></input></p>

这是我的 javascript 标签内容

function performAjaxSubmit() {

var sampleFile1 = document.getElementById("sampleFile5").files[0];
var formdata = new FormData();

formdata.append("sampleFile", sampleFile1);

var xhr = new XMLHttpRequest();

xhr.onload = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
// alert("ok..." + xhr.responseText);
//?????????????????????????????
document.location=xhr.responseText;
}
};

xhr.open("POST","/TestAjaxServletDownload/DownloadServlet", true);
xhr.send(formdata);

}

这是我的 web.xml serverlet 映射标签

<servlet>
<description></description>
<display-name>DownloadServlet</display-name>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>test.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/DownloadServlet</url-pattern>
</servlet-mapping>

这是我的 servlet 代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("DO GET SERVLET MERGE");
execute (request, response);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("DO POST SERVLET MERGE");
execute (request, response);
}


protected void execute(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
File downloadFile = new File("c:\\data\\example.pdf");
System.out.println("++++" + downloadFile.getAbsolutePath());
// System.out.println(uploadPathTemp+mergeFileName);
FileInputStream inStream = new FileInputStream(downloadFile);
// obtains ServletContext
ServletContext context = getServletContext();

// gets MIME type of the file
String mimeType = context.getMimeType(downloadFile.getCanonicalPath());
if (mimeType == null) {
// set to binary type if MIME mapping not found
mimeType = "application/octet-stream";
}

// modifies response
response.setContentType(mimeType);
response.setContentLength((int) downloadFile.length());

// forces download
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
System.out.println(downloadFile.getName());
response.setHeader(headerKey, headerValue);

// obtains response's output stream
OutputStream outStream = response.getOutputStream();

byte[] buffer = new byte[4096];
int bytesRead = -1;

while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}

inStream.close();
outStream.close();

}

最佳答案

改变怎么样

<a href="/TestAjaxServletDownload/DownloadServlet" >
download
</a>

<a id="pdfLink" href="/TestAjaxServletDownload/DownloadServlet" >
download
</a>

然后使用 document.getElementById('pdfLink').click()

关于javascript - 请求通过ajax下载servlet文件(请不要使用jquery..),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29556843/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com