Spring Rest和jQuery Ajax文件下载 [英] Spring Rest and jQuery Ajax File Download

查看:105
本文介绍了Spring Rest和jQuery Ajax文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用jQuery和Spring Rest. jQuery用于将文件上传和下载到服务器.上传过程运行正常,但是下载文件没有问题.因此,该方案是,在视图中,用户将选择n个要下载的文件,然后单击下载"按钮.用户单击该按钮后,将下载文件.我不想为每个文件下载打开一个新的新标签.我想在同一窗口下载而不刷新当前视图.我调查了但是对我没有多大帮助.有什么办法可以实现呢?

I am currently working with jQuery and Spring Rest. jQuery is used to upload and download files to the Server. The upload process is working fine but I have little problem with downloading the files. So the scenario is, in the view, the user will select the n numbers of files to download and clicks the download button. Once the user clicks that button, the files will be downloaded. I do not want to open a new new tab for each file download. I would like to download on the same window without refreshing the current view. I looked into this but didn't help me much. Is there any way, I can achieve this?

推荐答案

这是我下载文件的解决方案:

Here is my solution to download the file:

Spring Controller方法:

Spring Controller method:

@RequestMapping(value = "/download", method = RequestMethod.GET)
public void retrieveDocument(@RequestParam("id") String id, HttpServletResponse response) throws IOException {
    InputStream in = fileService.getFileStream(); // My service to get the stream.
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    response.setHeader("Content-Transfer-Encoding", "binary");
    response.setHeader("Content-Disposition", "attachment; filename=" + filename);
    try {
        IOUtils.copy(inputStream, response.getOuputStream()); //Apache commons IO.
        inputStream.close();
        response.flushBuffer();
        response.setStatus(HttpServletResponse.SC_OK);
    } catch (Exception e) {
        //log error.
    }

}

在客户端功能:

function download(id) {
    var id = $('#file').attr('id')
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'url here' + id, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function() {
        if(this.status == '200') {
           var filename = '';
           //get the filename from the header.
           var disposition = xhr.getResponseHeader('Content-Disposition');
           if (disposition && disposition.indexOf('attachment') !== -1) {
               var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
               var matches = filenameRegex.exec(disposition);
               if (matches !== null && matches[1])
                   filename = matches[1].replace(/['"]/g, '');
           }
           var type = xhr.getResponseHeader('Content-Type');
           var blob = new Blob([this.response],  {type: type});
           //workaround for IE
           if(typeof window.navigator.msSaveBlob != 'undefined') {
               window.navigator.msSaveBlob(blob, filename);
           }
           else {
               var URL = window.URL || window.webkitURL;
               var download_URL = URL.createObjectURL(blob);
               if(filename) {
                   var a_link = document.createElement('a');
                   if(typeof a_link.download == 'undefined') {
                       window.location = download_URL;
                   }else {
                       a_link.href = download_URL;
                       a_link.download = filename;
                       document.body.appendChild(a_link);
                       a_link.click();
                   }
               }else {
                   window.location = download_URL;
               }
               setTimeout(function() {
                   URL.revokeObjectURL(download_URL);
               }, 10000);
           }
        }else {
            alert('error')';//do something...
        }
    }; 
    xhr.setRequestHeader('Content-type', 'application/*');
    xhr.send();
}

这篇关于Spring Rest和jQuery Ajax文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆