Reputation: 13
I have a SPA and a WebAPI.
The user clicks a link on the SPA which is meant to download 2 files (a PDF and a XFDF).
I have this WebAPI action (source: What's the best way to serve up multiple binary files from a single WebApi method?)
[HttpGet]
[Route("/api/files/both/{id}")]
public HttpResponseMessage GetBothFiles([FromRoute][Required]string id)
{
StreamContent pdfContent =null;
{
var path = "location of PDF on server";
var stream = new FileStream(path, FileMode.Open);
pdfContent = new StreamContent(stream);
pdfContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/vnd.adobe.xfdf");
}
StreamContent xfdfContent = null;
{
var path = "location of XFDF on server";
var stream = new FileStream(path, FileMode.Open);
xfdfContent = new StreamContent(stream);
xfdfContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
}
var content = new MultipartContent();
content.Add(pdfContent);
content.Add(xfdfContent);
var response = new HttpResponseMessage();
response.Content = content;
return response;
}
In the SPA I do this
window.location.href = "/api/files/both/5";
The result. In the browser this JSON is shown
{
"Version": "1.1",
"Content": [{
"Headers": [{
"Key": "Content-Type",
"Value": ["application/vnd.adobe.xfdf"]
}
]
}, {
"Headers": [{
"Key": "Content-Type",
"Value": ["application/pdf"]
}
]
}
],
"StatusCode": 200,
"ReasonPhrase": "OK",
"Headers": [],
"TrailingHeaders": [],
"RequestMessage": null,
"IsSuccessStatusCode": true
}
The reponse header is (notice that content-type = application/json)
HTTP/1.1 200 OK
x-powered-by: ASP.NET
content-length: 290
content-type: application/json; charset=utf-8
server: Microsoft-IIS/10.0
request-context: appId=cid-v1:e6b3643a-19a5-4605-a657-5e7333e7b99a
date: Tue, 04 Feb 2020 11:31:49 GMT
connection: close
Vary: Accept-Encoding
The original request header (in case it is of interest)
Host: localhost:8101
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:8101/
DNT: 1
Connection: keep-alive
Cookie: MySession=....
Upgrade-Insecure-Requests: 1
Question
Upvotes: 1
Views: 311
Reputation: 368
Another option is to compress both/multiple files and return it as single file.
Upvotes: 0
Reputation: 618
You can't return 2 different files in the same request. You could embed the content into a json object which contains 2 things, but then you'd have to work out a way of displaying the files, or you could return the URI of the 2 files and then make the 2 requests for the files individually.
Personally I'd do the latter option, as it's the easiest and most flexible depending on what you plan on doing with the files.
Hope that helps
Upvotes: 4