webapi 文件下载输出接口
public async ask<HttpResponseMessage> GetGuidingPrincipleDownload(string name )
{
bool status = false;
try
{
if (name != "")
{
name += name + ".pdf";
status = connectState(@"\\10.10.10.178\phiic_file_hive\fdaBioEquiTestGuide", @"anonymous", "");//连接共享文件 connectState( 文件 用户名 密码)
if (status)
{
DirectoryInfo theFolder = new DirectoryInfo(@"\\10.10.10.178\phiic_file_hive\fdaBioEquiTestGuide\");
string path = Path.Combine(theFolder.ToString(), name);//路径
if (!string.IsNullOrWhiteSpace(path) && File.Exists(path))
{
string filename = Path.GetFileName(path);
var stream = new FileStream(path, FileMode.Open, FileAccess.Read);//打开文件
HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(stream)
};
resp.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = filename
};
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
resp.Content.Headers.ContentLength = stream.Length;
return await Task.FromResult(resp);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
public static bool connectState(string path, string userName, string passWord)
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
//proc.StandardInput.WriteLine(@"Net Use {0} /del", path); //必须先删除,否则报错
string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit();
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
Flag = true;
}
else
{
throw new Exception(errormsg);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
webapi 文件下载输出接口的更多相关文章
- C# asp.net webapi下支持文件下载输出接口
/// <summary> /// 下载文件 /// </summary> public class DownloadController : ApiC ...
- WebApi统一输出接口
public class WebApi { /// <summary> /// 成功后的输出 /// </summary> /// <param name="d ...
- ASP.NET CORE WEBAPI文件下载
ASP.NET CORE WEBAPI文件下载 最近要使用ASP.NET CORE WEBAPI用来下载文件,使用的.NET CORE 3.1.考虑如下场景: 文件是程序生成的. 文件应该能兼容各种格 ...
- H5+Ajax+WebApi实现文件下载(进度条,多文件)
前言 踩过的坑 1.WebAPI跨域 2.Jquery ajax低版本不支持XHR 2功能 3.Jquery ajax不支持Deferred的process事件 4.IE下文件名乱码问题 功能实现 & ...
- .net core webapi带权限的文件下载方法
众所周知,在webapi中,如果有个接口需要权限,一般会将带权限的字段塞进header中.但是,在带权限的文档下载接口中,无论是用post,还是get方式,我们无法设置header头信息.苦恼呀?别急 ...
- webapi 下载Ftp文件并返回流到浏览器完成文件下载
ResultModel<HttpResponseMessage> resultModel = new ResultModel<HttpResponseMessage>(Resu ...
- ASP.NET WebAPi之断点续传下载(下)
前言 上一篇我们穿插了C#的内容,本篇我们继续来讲讲webapi中断点续传的其他情况以及利用webclient来实现断点续传,至此关于webapi断点续传下载以及上传内容都已经全部完结,一直嚷嚷着把S ...
- ASP.NET WebAPi之断点续传下载(中)
前言 前情回顾:上一篇我们遗留了两个问题,一个是未完全实现断点续传,另外则是在响应时是返回StreamContent还是PushStreamContent呢?这一节我们重点来解决这两个问题,同时就在此 ...
- 如何在启用JWT Token授权的.NET Core WebApi项目中下载文件
背景 前几天,做项目的时候遇到一个文件下载的问题.当前系统是一个前后端分离的项目,前端是一个AngularJs项目, 后端是一个.NET Core WebApi项目.后端的Api项目使用了Jwt To ...
随机推荐
- with语句与__enter__,__exit__
class Foo(object): def func(self): print("func") pass def __enter__(self): print("ent ...
- web http协议
http协议超文本传输协议 http协议是IOS七层协议的应用层,是基于TCP/IP协议的,为什么还要多一个协议了,其实利用TCP协议也是可以的,但是TCP三次握手后是一直保持连接的,如果单单是c/s ...
- 学号20155308 2016-2017-2 《Java程序设计》第7周学习总结
学号20155308 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十二章 使用Optional代替null 标准API的函数接口 API 功能 Cons ...
- cocos2dx 3.x 拼图小游戏
.h #define IMAGE_MAX 2 //图片的个数.. //图片结构体 属性 struct IMAGE_DATA { cocos2d::Sprite *m_pImage; bool m_bO ...
- uwsgi手动安装时报错ValueError: invalid literal for int() with base 10: '32_1'
安装uwsgi,安装步骤如下 wget https://projects.unbit.it/downloads/uwsgi-latest.tar.gz tar zxvf uwsgi-latest.ta ...
- bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)
P2912 [USACO08OCT]牧场散步Pasture Walking 求树上两点间路径--->lca 使用倍增处理lca(树剖多长鸭) #include<iostream> # ...
- 常用<meta>
转自:http://segmentfault.com/a/1190000002407912 w3c -- <meta>标签:http://www.w3school.com.cn/tags/ ...
- 试着用React写项目-利用Webpack搭环境
转载请注明出处:王亟亟的大牛之路 最近都蛋疼,然后前些天开了个会就是关于"不加班就得死"的死命令,作为抵制加班的先头兵,我感觉我时日无多是时候加快武装自己的速度不然吃土都不配了,就 ...
- 【问题解决:未找到端口号】启动报错Circular placeholder reference 'server.port' in property definitions
问题描述: 启动spring boot项目时报错:Circular placeholder reference 'server.port' in property definitions 解决过程: ...
- 服务器jupyter配置与ssh远程登录
jupyter 配置 首先安装jupyter,在anaconda套装中已包含,如果安装的是精简版的miniconda则通过conda install jupyter安装. 生成配置文件 jupyter ...