jquery - 使用 ajax 下载 .net core 生成的 pdf 文件
问题描述
我正在努力使用 ajax 调用下载带有 .net 核心的生成的 pdf。基本上我有一个用户需要选择然后打印 pdf 输出的订单列表。我可以使用带有操作链接的按钮来做到这一点,但我需要以某种方式传入选定的值(选定的复选框),因此我使用 ajax 并传入这些值。
到目前为止,我有这个:
html:
<button id="btnPrintSelectedOrdersDeliveryLabels">Print labels</button>
C#:
return File(Convert.FromBase64String(deliveryAPIprintLabelsResult.Labels), "application/pdf", "delivery_labels.pdf");
javascript:
$.ajax({
method: "GET",
url: '@Url.Action("PrintDeliveryLabelsByOrderId", "Delivery")',
cache: false,
data: {
orderIds: JSON.stringify(orderIds)
},
success: function(result) {
var blob = new Blob([result], { type: 'application/pdf' });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = 'delivery_labels.pdf';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
},
error: function() {
console.log('error!');
}
});
}
我很确定错误在 ajax 成功调用中的某个地方,因为我得到了一个 pdf 但它是空白的。
解决方案
我想到了。Jquery 不支持“blob”,所以最终的解决方案应该使用 XHR。
推荐阅读
- java - 减少在 selenium 上的屏幕截图时间
- r - 如何分配门票
- node.js - 在第一次自动使用之后维护 google 客户端 API refresh_token
- java - 如何满足具有布尔条件的谓词
- http - Go http handler 路由未注册,路由不一致
- linux - 如何在linux中水平打印awk输出
- java - org.json.JSONException:java.lang.String 类型的 HTTP 值无法转换为 JSONObject
- c++ - 是否应该为类中的每个 setter 设置互斥锁,或者使所有成员变量原子化以实现线程安全?
- json - 如何解码这个json字符串
- java - 当实际的jvm安装发生时?