简体   繁体   English

从另一个服务下载 ServiceStack WebApi 文件(传递)

[英]ServiceStack WebApi File Download From Another Service (Pass-Through)

Summary: I have an application service/API and a reporting service/API;总结:我有一个应用服务/API 和一个报告服务/API; mutually exclusive of each other (on different servers/environments; app service does not have access to reporting service file system).相互排斥(在不同的服务器/环境上;应用服务无权访问报告服务文件系统)。 My front-end app calls the app service to build a report.我的前端应用调用应用服务来构建报告。 The app service passes the BLL data that the report needs to the reporting service, which then builds the report and sends it back as an API.应用服务将报告所需的 BLL 数据传递给报告服务,然后报告服务构建报告并将其作为 API 发送回。 The app service then needs to pass along that PDF file and have it download from the browser.然后,应用服务需要传递该 PDF 文件并从浏览器下载它。

The trouble I am having is the pass-through and getting the app service to download the file that the reporting service creats.我遇到的问题是传递并让应用服务下载报告服务创建的文件。 The reporting service is confirmed successfully building the report.报告服务已确认成功构建报告。 If I call in to the reporting service directly, I get the pdf download.如果我直接致电报告服务,我将获得 pdf 下载。 However, when I call in through the app service, my response is a bunch of jumbled mess instead of being a file download.然而,当我通过应用服务调用时,我的反应是一堆乱七八糟的东西,而不是文件下载。

Both services use ServiceStack on WebAPI, c# 6.这两个服务都在 WebAPI、c# 6 上使用 ServiceStack。

Here is a snippet of the response text:这是响应文本的片段:

%PDF-1.2 
%âãÏÓ 
1 0 obj 
<< 
/Type /Catalog 
/Pages 2 0 R 
/PageMode /UseNone 
/ViewerPreferences << 
/FitWindow true 
/PageLayout /SinglePage 
/NonFullScreenPageMode /UseNone 
>> 
>> 
endobj 
5 0 obj 
<< 
/Length 5964 
/Filter [ /FlateDecode ] 
>> 
stream
xµ]ÛrÜF}çWTļ4#D³î½IdÆthDywg­  DBT¯ÝÝÔüÇ~ðÃÄüÀf]d±q)zl;,<@¢pêdVV¢X¢ßPE(ü{Bãq\ÞÑðÃíu:xóí2øÿ£Ö&oë£ÿ>j8yuÄ)ùÇr0-%aÔYJ¤Vþp/ù;E¥ñçp5ß]iΦ<ܸþGÆ
oÑHr¹plü­¦µâIxBIJ¾!.`áÄ7;ÚJx   ­<9u2 VPÖ!áw÷8&ûfãþQN
ÖÂ1§:Z3\¾éáZ)bû¨Pý]ñ¤oDOÕÍQd»kCâÞéøNÚ¥³£cálÀdÉcÆæ,ÂI((ppXpÒ#####
0y0Y09Ð9Ð9PÎ '=¢1¢1"0"0B1B"
BÂIHH00pÒ#
#
##!sÀ1sÀ1sÀ;7
|O÷ÇSAeç.áAàt=äHË:(#/ó²ìüÏßÔÂii10jrcLbÌ9±Òäl¨¡á±f@@ÈÑÂ"0b6# LFA,H0q 34â@g(Cûø)"¢3!D`DZÐQ"MpHÀ6 ,Cô!!*C(B8F8âgpÄÏ8àÞq`¹
ÂÑë&º-ƨÉ111ç0Ä2H³"¢2"cĸ!!#1a3"âÁd4ÄÉHÐq :ã@#tÆ2!LÆcgB$Bt0H!j@¤ÉáÂ2D°"¢2"c#xÆGð8à1íK?ô?a
a}ÄO§cc)â'e >âÇ3!!#)âÇc!!:CBFbÂfDÄÉh0     &ã@#tÆFèñãqíâDÎñ#B3D

I don't think I need to post too much about the reporting service since that does not appear to be the issue.我不认为我需要发布太多关于报告服务的信息,因为这似乎不是问题。 Here is the Post from that side, though:不过,这是来自那一侧的帖子:

[AddHeader(ContentType = "application/pdf")]
public object Post(ClaimInformationFormRequest request)
{
    var report = new ClaimInformationForm(request);
    return report.GetReport();
}

And on the app service side, things aren't much different:而在应用服务端,情况也没有太大不同:

[AddHeader(ContentType = "application/pdf")]
public object Any(ClaimInformationFormReportRequest req)
{
    //Omitted from this sample... it's BLL that creates the 'messageData' and other params/vars; works fine

    var res = new { SponsorId = paramSponsorId, ClaimMonth = paramMonth, ClaimYear = paramYear, Data = messageData };

    var client2 = ServiceClientFactory.GetJsonClient(ExternalServicesHelper.GetReportsUrl());
    client2.UseBasicAuth(UserSessionState.GetBasicAuthHeader());

    return client2.Post<byte[]>("/homes/ClaimProcessing/ClaimInformationForm", res);
}

I have tried a wide variety of streams, byte readings, etc. I've set up my code like many search result samples to no avail, though I have yet to find an example of passing a file between two different services.我尝试了各种各样的流、字节读取等。我已经像许多搜索结果示例一样设置了我的代码,但无济于事,尽管我还没有找到在两个不同服务之间传递文件的示例。 I'm sure I am missing something on the pass-through between the services, but I'm not sure.我确定我在服务之间的传递中遗漏了一些东西,但我不确定。

After looking over the Fiddler captures as Mythz suggested and getting the headers all lined up, I realized that the application service was returning a Content-Type of application/octet-stream.在查看了 Mythz 建议的 Fiddler 捕获并获得所有标题后,我意识到应用程序服务正在返回应用程序/八位字节流的内容类型。 The solution for this was to change the way I return the information from the application service.对此的解决方案是改变我从应用程序服务返回信息的方式。

Changed this:改变了这一点:

return client2.Post<byte[]>("/homes/ClaimProcessing/ClaimInformationForm", res);

To this:对此:

var result = client2.Post<byte[]>("/homes/ClaimProcessing/ClaimInformationForm", res);
return new HttpResult(result, "application/pdf");

I then received the file save dialog and the subsequent PDF.然后我收到了文件保存对话框和随后的 PDF。

The text.txt can also be a stream text.txt 也可以是流

var result = new HttpResult("text.txt", "application/pdf");
result.Headers.Add("Content-Disposition", "attachment;text.txt;");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM