react backend and frontend download file
2024-10-21 09:32:21
import { View as ViewFile} from '@/api/SafetyRule';
const Handler_DownLoadFile = (Id:number,IsEnglish:boolean)=>{
sfParam.Id=Id;
sfParam.IsEnglish=IsEnglish;
setSFParam(sfParam);
console.log(JSON.stringify(sfParam))
ViewFile(sfParam as SafetyRuleParamPO)
.then((res)=>{
if (res.StatusCode == 200 && res.Data){
if(res.Data.FileContent!=undefined){
const blob = UtilFile.ConvertBase64UrlToBlob(res.Data.FileContent);
const url = window.URL.createObjectURL(
new Blob([blob],{type:'application/pdf'})
);
const link = document.createElement('a');
link.href = url;
link.setAttribute(
'download',
"FileName.pdf",
);
// Append to html link element page
document.body.appendChild(link);
// Start download
link.click();
window.URL.revokeObjectURL(url);
}
}else{
alert("error")
}
})
}
export function ConvertBase64UrlToBlob(base64Data):Blob {
const byteCharacters = atob(base64Data);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], {type: 'application/pdf'});
return blob;
}
import Request from '@/api/request';
import ApiResponsePO from "@/model/ResponseDTO/ApiResponseDTO"; import SafetyRuleViewPO from '@/model/PO/SafetyRuleViewPO' const http = new Request(); export const urlViewFile = '/api/SafetyRule/View' export const View = (params: SafetyRuleParamPO): Promise<ApiResponsePO<SafetyRuleViewPO>> => {
return http.get<object, ApiResponsePO<SafetyRuleViewPO>>(urlViewFile,params);
}
recommen a :base64 to blob Code Example (codegrepper.com)
backend
public SafetyRuleViewVO ReadFile(int Id,bool IsEnglish) {
var safetyVO = GetById(Id);
var viewVO = new SafetyRuleViewVO();
if (safetyVO != null) {
string filePath = IsEnglish ? safetyVO.EnglishVersionFileSharePointPath : safetyVO.ChineseVersionFileSharePointPath;
byte[] fileContent = File.ReadAllBytes(filePath);
viewVO = new SafetyRuleViewVO {
FileContent = fileContent
};
}
return viewVO;
}
public class SafetyRuleViewVO {
public byte[] FileContent { get; set; }
}
最新文章
- CodingLife主题更新
- 关于调整浏览器窗口JS
- OJ-上海交大-1021. 从前有座山
- 关于ztree异步加载的问题(二)
- python中使用list作为默认参数且调用时不给其赋值的问题
- 感受函数式编程-scala
- 如何打开&ldquo;USB调试&rdquo;模式?
- linux 定时任务计划
- Linux下服务器环境的搭建和配置之一——Apache篇
- 转:检查点(web_reg_find函数详解)
- 【批处理学习笔记】第十二课:常用DOS命令(2)
- 腾讯云服务器php+mysq+nginx配置出现的问题及解决方法(亲测)
- javascript alert乱码的解决方法
- Spring Boot(十)Logback和Log4j2集成与日志发展史
- ps常用指令集
- group by分组后获得每组中符合条件的那条记录
- docker资料---仓库搭建
- SpringBoot文件上传下载
- yum安装mysql-5.6(centos7)
- 出现Unable to locate appropriate constructor on class 错误可能的原因
热门文章
- UE4笔记索引
- Lucky Chains(最大公约数的应用)
- API的风格
- docker指令 —— MySQL一条龙服务
- <;input>;输入框,限制输入的为正整数
- 使用async实现多个请求并发
- 可收集ALC问题[Microsoft.Data.SqlClient is not supported on this platform.]
- 【笔记】IDEA中maven导入依赖提示证书错误解决方法
- 读后笔记 -- Java核心技术(第11版 卷 II) Chapter2 输入与输出
- QueryDet: Cascaded Sparse Query for Accelerating High-Resolution Small Object Detection(QueryDet:用于加速高分辨率小目标检测的级联稀疏查询)