使用需要标头并提供内容的REST请求下载文件 [英] Download file with a REST request needing headers and giving the content

查看:75
本文介绍了使用需要标头并提供内容的REST请求下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有REST API的AngularJs。我没有掌握REST API。
我可以通过发送REST请求来存储带有API的数字对象。
我也可以通过GET请求得到它。
请求需要有一些特定的标题。

I am using AngularJs with a REST API. I don't have the hand on the REST API. I can store digital object with the API by sending a REST request. I can get it also with a GET request. The requests needs to have some specific headers.

我的目标是为用户提供下载并另存为链接。
现在点击事件我发出请求:

My goal is to give the user a "download and save as" link. For now on the click event i make the request :

    this.file = function (file) {
        var url = config.domain + 'file/' + file;

        var methods = resource(url, null, { 
            'get': {
                method:'GET', 
                headers:{   'Authorization' : user.auth, 
                            'secret-key' : user.secretkey}
            }
            transformResponse : function(data, headersGetter){
                                    return {content:data}; //transform octet stream into text, angular returns an array containing 1 character per element.
                                },
        });
        return methods;
    };

在退货正文中我有文件内容(见下文)。我想下载它。这怎么可能 ?请注意,我无法将文件存储为URL。

in the return body I have the file content (see below). I would like to download it. How is it possible ? Notice that I can't store the file as a URL.

是否可以打开一个窗口,使用好标题进行其余调用并保存文件?

Would it be possible to open a window wich make the rest call with the good headers and save the file ?

编辑

我需要解决方案才能与50Mo文件配合使用。

I need the solution to be able to work well with a 50Mo File.

我拥有的PDF文件内容示例:

example of a PDF file content I have :

%PDF-1.7
£´ÅÖçø
2 0 obj
[/ICCBased 3 0 R]
endobj
3 0 obj
<<
/Filter /FlateDecode 
/Length 2596 
/N 3 
>>
stream
xwTSÙϽ7½PÐkhRH
½H.*1   JÀ"6DTpDQ¦2(à£C±"Q±ëDÔqpId­ß¼yïÍß÷~k½ÏÝgï}ÖºüÂLX    ¡XáçÅg`ðlàp³³BøF|Ølø½º         ùû*Ó?Áÿ¹Y"1PçòøÙ\É8=W%·Oɶ4MÎ0JÎ"Y2Vsò,[|öe9ó2<ËsÎâeðäÜ'ã9¾`çø¹2¾&ctI@Æoä±|N6(Ü.æsSdl-c(2-        ãyàHÉ_ðÒ/XÌÏËÅÎÌZ.$§&\SáÏÏMçÅÌ07#â1ØYárfÏüYym²";Ø8980m-m¾(Ô]ü÷v^îDøÃöW~
°¦eµÙúmi]ëP»ýÍ`/²¾u}qº|^RÄâ,g+«ÜÜ\Kk)/èïúC_|ÏR¾Ýïåaxó8t1C^7nfz¦DÄÈÎâpùæøþuü$¾/ED˦L         Lµ[ÈB@øøÃþ¤Ù¹ÚøÐX¥!@~(*     {d+Ðï}ÆGùÍÑûÏþ}W¸LþÈ$cGD2¸QÎìüZ4 E@ê@èÀ¶À¸àA(q`1àD µ ­`'¨u     46ptcà48.Ë`ÜR0)ð
Ì@ÈRt CȲXäCP%CBH@ë R¨ªê¡fè[è(tº
C· Qhúz#0   ¦ÁZ°l³`O8ÁÉð28.·Àp|îOÃàX
?§:¢0ÂFBx$  !«¤i@Ú¤¹H§È[EE1PLÊ⢡V¡6£ªQP¨>ÔUÔ(j
õMFk¢ÍÑÎèt,:.FW Ðè³èô8ú¡c1L&³³³Ó9Æa¦±X¬:Öë
År°bl1¶
{{{;}#âtp¶8_\<N+ÄU
[.....]


推荐答案

感谢大家帮忙找到解决方案。

Thanks everybody for helping find a solution.

我无法找到在javascript和客户端应用程序中满足解决方案。
我将使代理类与API通信。

I am not able to find a satisfying solution in javascript and client side application. I am going to make a proxy class communicating with the API.

这将发送带有安全头的REST请求并将作为响应提供文件内容。

This will send the REST request with security headers and will give as response the file content.

此类将由HTTP GET请求调用,因此,由于正确的响应,保存为过程将很容易管理标题。

This class will be called by a HTTP GET request, so the 'save as' process will be managed easily thanks to right response headers.

这篇关于使用需要标头并提供内容的REST请求下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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