vue下载压缩文件

编程入门 行业动态 更新时间:2024-10-25 22:30:15

vue下载<a href=https://www.elefans.com/category/jswz/34/1758321.html style=压缩文件"/>

vue下载压缩文件

C#+vue 下载压缩文件

  1. 后台C#代码
#region 压缩文件[Route("api/sys/DownloadFiles")][HttpGet]public HttpResponseMessage DownloadFiles(string path){try{string batchCode = path;path = FileHelper.LocalPath + "\\" + path;if (!Directory.Exists(path)){return null;}var zipFileUrl = FileHelper.LocalPath + batchCode + ".zip";if (File.Exists(zipFileUrl)){File.Delete(zipFileUrl);}CreateZipFile(path, zipFileUrl);var stream = new FileStream(zipFileUrl, FileMode.Open);HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);response.Content = new StreamContent(stream);response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");return response;}catch (Exception ex){return null;}}/// 压缩成zip/// </summary>/// <param name="folderToZip">d:\</param>/// <param name="zipedFile">d:\a.zip</param>public static void CreateZipFile(string folderToZip, string zipedFile){bool result = false;if (!Directory.Exists(folderToZip))return;ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile));zipStream.SetLevel(6);//if (!string.IsNullOrEmpty(password)) zipStream.Password = password;result = ZipDirectory(folderToZip, zipStream, "");zipStream.Finish();zipStream.Close();return;}/// <summary>   /// 递归压缩文件夹的内部方法   /// </summary>   /// <param name="folderToZip">要压缩的文件夹路径</param>   /// <param name="zipStream">压缩输出流</param>   /// <param name="parentFolderName">此文件夹的上级文件夹</param>   /// <returns></returns>   private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName){bool result = true;string[] folders, files;ZipEntry ent = null;FileStream fs = null;try{ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/"));zipStream.PutNextEntry(ent);zipStream.Flush();files = Directory.GetFiles(folderToZip);foreach (string file in files){fs = File.OpenRead(file);byte[] buffer = new byte[fs.Length];fs.Read(buffer, 0, buffer.Length);ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file)));ent.DateTime = DateTime.Now;ent.Size = fs.Length;fs.Close();zipStream.PutNextEntry(ent);zipStream.Write(buffer, 0, buffer.Length);}}catch{result = false;}finally{if (fs != null){fs.Close();fs.Dispose();}if (ent != null){ent = null;}GC.Collect();GC.Collect(1);}folders = Directory.GetDirectories(folderToZip);foreach (string folder in folders)if (!ZipDirectory(folder, zipStream, Path.GetFileName(folderToZip)))return false;return result;}#endregion
  1. 前台VUE代码
    安装 blob: npm install blob
    安装 axios:npm install axios --save
    引用:import axios from ‘axios’
    import Blob from ‘blob’;
downloadFile() {let url=util.getUrl()+"/api/sys/DownloadFiles"this.loading = true;axios.get(url,//请求的url{params:{'path':XX //传参},responseType:'blob'//服务器返回的数据类型}).then((res)=>{// 处理返回的文件流const content = res.data;const blob = new Blob([content], { type: "application/zip" });const fileName = this.bathCode+".zip";if ("download" in document.createElement("a")) {// 非IE下载const elink = document.createElement("a");elink.download = fileName;elink.style.display = "none";elink.href = URL.createObjectURL(blob);document.body.appendChild(elink);elink.click();URL.revokeObjectURL(elink.href); // 释放URL 对象document.body.removeChild(elink);} else {// IE10+下载navigator.msSaveBlob(blob, fileName);}this.loading = false;});

更多推荐

vue下载压缩文件

本文发布于:2024-02-11 16:52:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1682110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:压缩文件   vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!