在模态对话中打开文件

时间:2016-10-04 13:48:42

标签: java file modal-dialog response

我有这个代码用于打开路径中的任何文件。但在这种情况下,文件将在新选项卡中打开。

我需要该响应在模态引导程序或对话框中打开此文件。

File file = new File(path);
String name = file.getName();
int fileSize = (int) file.length();
response.setContentType(contentType);
response.setContentLength(sizeFile);
response.setHeader("Content-Disposition", "inline; filename=\"" + name + "\"");
output = response.getOutputStream();
Files.copy(file.toPath(), output);

有人有提示或其他方法吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

使用bootstrap 3.3.5

在HTML中,按钮调用javascript函数' view'将参数作为参数传递给servlet(MSD)和文件扩展名:

<button class="btn-doc-sm btn-default" type="button" 
    onclick="view('<% out.print(MSD?p=c:/upload/documents/document.docx&action=view)); %>', '<% out.print(ext); %>')" id="view">
   <span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>View
</button>

.js函数设置pathDoc标识的模态的路径值,并打开&#39;视图&#39;模态的。

function view(path, ext) {
        var booleanValue;
        if (path !== null) {
            $("#pathDoc").attr("src", path);
            $("#view").modal('show');
            booleanValue = true;
        }
        if (booleanValue !== true) {
            $("#error").modal('show');
        }
    }

模态:

    <div class="modal fade" id="view" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                    <!-- Header -->
                    <div id="confirmacao-label1">
                        <a href="#close" title="Close" data-dismiss="modal" class="closeModal">X</a>
                    </div>
                        <!-- End header -->
                    <div class="modal-body">
                        <div class="embed-responsive embed-responsive-4by3" id="modal-embed">
                            <iframe class="embed-responsive-item" id="pathDoc" nome="pathDoc" src=""></iframe>
                        </div>
                    </div>
            </div>
        </div>
    </div>

&#39>变量路径&#39;是一个调用servlet传递“真实路径”的链接。并将动作作为参数。 servlet将打开带有文件的输出。

switch(action){
            case "view":
                try {
                    File file = new File(filePath);
                    String name = file.getName();
                    int fileSize = (int) file.length();
                    response.setContentType(action);
                    response.setContentLength(fileSize);
                    response.setHeader("Content-Disposition", "inline; filename=\"" + name + "\"");
                    output = response.getOutputStream();
                    Files.copy(file.toPath(), output);
                    break;
                } catch (Exception ex ) {
                    System.out.println(ex.getMessage());
                } finally {
                    output.close();
                }
                break;

因此,当调用Modal时,在src上设置了一个指向servlet的链接以打开文件。 servlet在输出时搜索并打开文件。

相关问题