在新窗口中显示使用jquery从控制器返回的PDF流 [英] Display PDF stream returned from controller using jquery in new window
问题描述
我有一个控制器动作,该动作从天蓝色的blobstorage中读取.pdf
文件,并将流对象返回给$.ajax()
方法.
I have a controller action which reads a .pdf
file from azure blobstorage and returns stream object to the $.ajax()
method.
控制器返回
var stream = blobStorage.OpenRead(filepath);
await FileAsync(stream, "application/pdf");
Ajax调用和响应
$.ajax({
type: "POST",
url: "/PDFfile",
data: { 'id': Id },
dataType: "application/pdf",
traditional: true,
complete: function(data) {
var w = window.open("data:application/pdf, " + escape(data.responseText));
w.document.write(data.responseText);
w.document.close();
}
});
但是,如果调用与所示的相同的控制器动作,则文件将正确显示.
However, if the call is to the same controller action as shown, the file is displayed correctly.
<a class="pull-right btn-link" target="_blank" data-bind="attr: { href: '/PDFfile/' + Id }"></a>
它可能是一个重复的问题,但没有一个解决方案对我有用. 我得到的输出如下所示.
It may appear as a duplicate question but none of the solutions worked for me. The output I am getting is as shown.
推荐答案
我找不到此问题的确切答案.我发现了很多不同的问题.
我终于可以使用fetch
拼凑起来.
您需要接受类型'arraybuffer'
,然后使用resopnse.blob()
变成斑点.然后,您可以在新窗口中打开Blob.
I couldnt find a exact answer to this question. I found a bunch of different questions.
I finally was able to piece together by using fetch
.
You need to accept type 'arraybuffer'
, then you turn into a blob with resopnse.blob()
. Then you can open the blob in a new window.
fetch('/post/url', {
method: 'POST',
headers: {
Accept: 'arraybuffer',
'Content-Type': 'application/json',
},
//you need to use json here
body: JSON.stringify({ items: [{ ItemId: 3, Quantity: 40 }], fileId: 2 });
})
.then(response => response.blob())
.then(data => {
var popup = window.open(URL.createObjectURL(data), "height=500,width=750);
});
这篇关于在新窗口中显示使用jquery从控制器返回的PDF流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!