Vue通过Web API下载本地服务器文件

news/2024/7/10 0:38:55 标签: vue, api, c#

后台api接口

 [HttpGet]
        public HttpResponseMessage DownloadFile(string fileName)
        {
            //fileName = "123.txt";
            //文件的服务器地址
            string filePath = HttpContext.Current.Server.MapPath("~/kaoqinwj/"+fileName);//路径
            FileStream stream = new FileStream(filePath, 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")
            {
                FileName = HttpUtility.UrlEncode(fileName)
            };
            response.Headers.Add("Access-Control-Expose-Headers", "FileName");
            response.Headers.Add("FileName", HttpUtility.UrlEncode(fileName));
            return response;
        }

我这是传了一个文件名称
前台vue: 不传参的话可以直接使用 a 标签

<el-button type="text" icon="el-icon-download" @click="Xiazai(scope.row.storeName,scope.row.statisticsDate)">
                            下载该文件</el-button>
                            <!-- <a href="http://localhost:58248/api/WenjianXZ">下载文件</a> -->

axios传参

axios.get('/WenjianXZ?fileName='+file)
.then(res => {
    console.log(res)
    window.location.href = res.config.url;//接收下载
})
.catch(err => {
    console.error(err); 
})

http://www.niftyadmin.cn/n/957038.html

相关文章

vue2.6的还可以

<!DOCTYPE html> <html><div id"app"><select v-model"selectItem" change"selectFn($event)"><!--选择项的value值默认选择项文本 可动态绑定选择项的value值 更改v-model指令绑定数据--><option v-for"…

C#(Zxing)二维码生成 并去除白边

引入包 1.通过NuGet引入ZXing.Net 2.生成二维码并保存 public string ErWeMaPrint(string name)//内容{// 配置参数Dictionary<EncodeHintType, Object> hints new Dictionary<EncodeHintType, object>();hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCo…

还没找到bug

<!DOCTYPE html> <html><div id"app"><select v-model"selectItem" change"selectFn($event)"><!--选择项的value值默认选择项文本 可动态绑定选择项的value值 更改v-model指令绑定数据--><option v-for"…

ABP手动注入租户ID

我这边需求是在未登录情况下注册手动给Dto的tenantId赋值新增是会报tenantId不能为0错误&#xff0c;所以tenantId是不能这样赋值的 后面找资料发现了这句代码&#xff1a; unitOfWorkManager.Current.SetTenantId(tenantId)亲测有用 &#xff01;&#xff01;&#xff01; 不…

https://unpkg.com/vue@next无法访问,正不正常?

是不是导致vscode&#xff0c;总是出现bug的原因&#xff1f; 为啥我最近打开vscode&#xff0c;总是出现Uncaught ReferenceError: Vue is not defined的bug&#xff0c;重装了也不行&#xff0c;以前能运行的代码现在也运行不了了

查看Vue的版本和Vue/CLI的版本

查看Vue版本&#xff1a; 1.npm list vue 2.进入项目中package.json文件直接查看 查看Vue/cli版本&#xff1a; vue -V 或者 vue --version vue -V vue/cli 4.5.11 vue --version vue/cli 4.5.11 npm list vue /Users/ivyone └─┬ vue3.2.23 └─┬ vue/server-renderer…

就用vue-cli先试着做,可以。为什么 computed这个这里这样不对?

<template><div class"hello">我是哈蜜瓜<h1>{{ msg }}</h1><div>counter: {{ counter }}</div></div> </template><script> export default {name: HelloWorld,props: {msg: String},data() {return {counte…

点击一下counter增加1的做法代码

<template><div class"hello">我是哈蜜瓜<h1>{{ msg }}</h1><div click"counter">counter: {{ counter }} </div></div> </template><script> export default {name: HelloWorld,props: {msg: Str…