C#传输文件流,ajax获取文件流
【代码】C#传输文件流,ajax接收获取文件流。
·
//C#服务端
public FileResult CustomerContract_Download(int Id)
{
var s = 获取文件的Byte[]类型
var r = File(s, 字符串类型填写文件的MIME类型);
return r;
}
//前端ajax
$.ajax({
url: 接口地址,
type: "post",
async: true,
cache: false,
xhrFields: { responseType: "blob" },//如果不填写的话,接收文件不全,打开会失败或者报错
data: { Id: id },
success: function (res) {
console.log(res);
//处理json数据
if (res.code==1) {
layer.alert(res.msg, { icon: 5 });
} else {
//处理后台返回的流数据
const blob = new Blob([res]);
const fileName = 文件名称(带文件格式,如:1.zip);
const linkNode = document.createElement('a');
linkNode.download = fileName; //a标签的download属性规定下载文件的名称
linkNode.style.display = 'none';
linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
document.body.appendChild(linkNode);
linkNode.click(); //模拟在按钮上的一次鼠标单击
URL.revokeObjectURL(linkNode.href); // 释放URL 对象
document.body.removeChild(linkNode);
}
}
});

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)