如何重定向HTML表单提交到iframe

How can I redirect HTML form submission to an iframe?

本文关键字:iframe 表单提交 HTML 重定向      更新时间:2023-09-26

我正试图实现一个解决使用AJAX与多部分表单数据的问题。这一页看起来是个好主意。基本上,不是使用AJAX,而是将表单提交的输出重定向到iframe。然而,这并不适合我。下面是我的客户端HTML代码:

<form id="submitDocumentForm" target='upload_target' name="submitDocumentForm" enctype="multipart/form-data" action="MyServlet" method="POST">
   <input id="inputFile" type="file" name="inputFile"/>
   <input type="submit" value="Import Document" />
   <iframe id="upload_target" name="upload_target" src="" style="width:100;height:100;border:0px solid #fff;"></iframe>
</form>

服务器端处理似乎工作正常,但客户端响应(IE7)是弹出一个文件下载"你想保存这个文件,还是找到一个程序…"对话框。下面是相关的服务器端Java代码:

//handling of the file and exception handling omitted
JSONObject responseData = new JSONObject();
responseData.put("messages", "Upload complete");
PrintWriter pw = response.getWriter();
response.setHeader(RESP_HEADER_CACHE_CONTROL, RESP_HEADER_VAL_NO_CACHE);
response.setContentType("application/json");
pw.println(responseData.toString());
pw.close();

关于如何解决这个问题,有什么建议吗?

将响应的Content-Type设置为text/plain,以强制浏览器将其视为文档而不是文件。