本文介绍了角HTTP GET,从Spring MVC的服务器下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Apache IOUtils复制方法从服务器发送文件angularjs。
这是我的控制器:

  @RequestMapping(值=/下载,方法= RequestMethod.GET)
    公共无效downloadFile(HttpServletResponse的响应){        response.setContentType(图像/ JPG);        尝试{
            档案文件=新的文件(文件路径);            的InputStream的InputStream =新的FileInputStream(文件);
            IOUtils.copy(InputStream中,response.getOutputStream());        }赶上(...){
        .......
    }

在angularJs控制器:

  $ HTTP({            方法:GET,
            网址:'... /下载',
            标题:{内容类型:图像/ JPG'}        })        .success(功能(数据,状态){            的console.log(数据);
            VAR BLOB =新的Blob([数据] {类型:图像/ JPG'});
            的saveAs(一滴,'test.jpg放在');
        })        .error(功能(数据,状态){
            ....
        })

当我下载客户端的文件,我也没有看过。当我用记事本打开它++我发现特殊字符将被修改。

例如,当我打开与Notpad ++原文件,我得到这样一行:
òŽsCJVäl·²HWƒ...;¹(OE $ OO«AA'{取值€〜9ÎsŠÒogk

在同一行,当我打开下载的文件,用记事本++变为:
sCJVlHW;($Ӂҫ{ S〜 9sogk

然而,当我把直接下载链接(本地主机/所有MyApplication /下载)在浏览器中,它工作正常。文件都应该被加密,需要授权下载文件,所以我必须使用角度HTTP GET。

任何帮助将是AP preciated。


解决方案

我不得不responseTyp的添加到HTTP GET请求:

  $ HTTP({        方法:GET,
        网址:'... /下载',
        responseTyp的:arraybuffer
    })    .success(功能(数据,状态){        的console.log(数据);
        VAR BLOB =新的Blob([数据] {类型:图像/ JPG'});
        的saveAs(一滴,'test.jpg放在');
    })    .error(功能(数据,状态){
        ....
    })

现在它工作。

I'm using apache commons IOUtils copy method to send file from server to angularjs.This is my controller :

    @RequestMapping(value="/download", method = RequestMethod.GET)
    public void downloadFile(HttpServletResponse response) {

        response.setContentType("image/jpg");

        try {
            File file = new File(filePath);

            InputStream inputStream = new FileInputStream(file);
            IOUtils.copy(inputStream, response.getOutputStream());

        } catch (...) {
        .......
    }

In angularJs controller :

$http({

            method: 'GET',
            url: '.../download',
            headers: {'Content-Type': 'image/jpg'}

        })

        .success(function(data, status){

            console.log(data);
            var blob = new Blob([data], {type: 'image/jpg'});
            saveAs(blob, 'test.jpg');
        })

        .error(function(data, status){
            ....
        })

When I download the file in the client side, I can't read it. When I open it with notepad++ I find that special characters are modified.

For example, when I open the original file with Notpad++, I get a line like this :òŽsCJVäl·²HWƒ…;¹(òÈ$ÓÒ«Á‘{S€~9ÎsŠÒogk

The same line, when I open the downloaded file with notepad++ becomes :��sCJV�l��HW��;�(��$�Ӂҫ��{S�~9�s��ogk

However, when I put the download link (localhost/myApplication/download) directly in a browser, it works correctly. Files are supposed to be encrypted and authorization is needed to download a file, so I have to use angular HTTP get.

Any help would be appreciated.

解决方案

I had to add responseType to HTTP get request :

$http({

        method: 'GET',
        url: '.../download',
        responseType: 'arraybuffer'
    })

    .success(function(data, status){

        console.log(data);
        var blob = new Blob([data], {type: 'image/jpg'});
        saveAs(blob, 'test.jpg');
    })

    .error(function(data, status){
        ....
    })

Now it is working.

这篇关于角HTTP GET,从Spring MVC的服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:46