use*_*799 5 javascript asp.net ajax jquery window
我有一个控制器动作,它.pdf从azure blobstorage 读取一个文件并将stream对象返回给该$.ajax()方法.
控制器返回
var stream = blobStorage.OpenRead(filepath);
await FileAsync(stream, "application/pdf");
Run Code Online (Sandbox Code Playgroud)
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();
}
});
Run Code Online (Sandbox Code Playgroud)
但是,如果调用与所示的控制器操作相同,则文件会正确显示.
<a class="pull-right btn-link" target="_blank" data-bind="attr: { href: '/PDFfile/' + Id }"></a>
Run Code Online (Sandbox Code Playgroud)
Yva*_*van -2
为什么不直接使用标准 HTML 呢?这里不需要 Javascript(因为您<a>为初始操作触发器创建了一个标准标签)。
就像是:
<form action="/PDFfile" method="post" target="_blank">
<input type="submit" value="Show me the file in a new tab/window!"/>
</form>
Run Code Online (Sandbox Code Playgroud)
笔记:
Content-Type: application/pdf在 Azure 脚本中设置标头,以便浏览器知道它是 PDF