使用POST从API下载PDF

时间:2017-09-07 17:00:29

标签: javascript c# asp.net-web-api

我试图将带有一些标题信息和其他参数的请求发布到我的api,然后让它返回一个PDF文件。

我已经使API通信正常工作了但是我已经尝试了几种查看PDF的方法,但没有一种方法可以100%正常工作。 下面的方法是在stackoverflow上找到的,并以PDF格式返回响应数据,但是当它转换为blob以查看时它变为空白。

[API]
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
ReturnInformationITEM rtn = GetPDFData(); //Comes back as Base64
response.Content = new ByteArrayContent(System.Convert.FromBase64String(rtn.Data));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;


[client]
var blob=new Blob([result], {type: 'application/pdf'});
var pom = document.getElementById("pdf" + $counter); 
pom.setAttribute('href',window.URL.createObjectURL(blob));
pom.setAttribute('download',"Test.pdf");
pom.setAttribute('target','_blank');

'I did have a click event here but firefox ignores it so I have to pass the href back to the pom and manully click it'

如果我改变API以返回base64并让客户端在成为blob之前对其进行解码,那么它将显示页面但是带有丑陋的blob URL,并且在safari中保存时它不会识别' download'属性。

有没有人知道可以使用哪种方法可以跨浏览器工作并允许预先填充文件名?如果可能的话,在返回发布数据时自动显示文件。

由于

马克

0 个答案:

没有答案