ResultModel<HttpResponseMessage> resultModel = new ResultModel<HttpResponseMessage>(ResultStatus.Success);
FtpWebResponse ftpWebResponse = null;
Stream ftpStream = null;
try
{
if (dt != null && dt.Rows.Count > 0)
{
string content = dt.Rows[0]["Content"].ToString();
FileSourceDto fileSourceDto = Newtonsoft.Json.JsonConvert.DeserializeObject<FileSourceDto>(content);
string path = fileSourceDto.TargetInfo.FileUrl.TrimEnd('\\').TrimEnd('/') + "/" + fileSourceDto.TargetInfo.FileName;
string account = fileSourceDto.TargetInfo.UserName;
string pwd = fileSourceDto.TargetInfo.Password;
if (!string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(account) && !string.IsNullOrEmpty(pwd))
{
FtpWebRequest reqFTP;

// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));

// 指定数据传输类型
reqFTP.UseBinary = true;

// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(account, pwd);
ftpWebResponse = (FtpWebResponse)reqFTP.GetResponse();
}

ftpStream = ftpWebResponse.GetResponseStream();
byte[] bytes = null;
using (StreamReader sr = new StreamReader(ftpStream))
{
string fileContent = sr.ReadToEnd();
bytes = Encoding.UTF8.GetBytes(fileContent);
}
ftpStream = new MemoryStream(bytes);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(ftpStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileSourceDto.TargetInfo.FileName
};

resultModel.Data = response;
}
}
catch (Exception ex)
{
resultModel = new ResultModel<HttpResponseMessage>(ResultStatus.Fail, "JD_DataShare_ESB_Error[获取文件流]:" + ex.Message);
}
finally
{
if (ftpWebResponse != null)
{
ftpWebResponse.Close();
}
}

return resultModel;

webapi 下载Ftp文件并返回流到浏览器完成文件下载的更多相关文章

  1. WebApi下载附件文件

    WebApi下载附件文件 1. [RoutePrefix("down")] public class FilesController : ApiController { [GET( ...

  2. php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页面存储到本地

    php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页面存储到本地 yeah,搞定啦 php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页 ...

  3. 解决下载ftp文件过程中,浏览器直接解析文件(txt,png等)的问题

    搭建了一个ftp服务器,供用户进行上传下载,在下载过程中发现,一些文件,例如txt,jpg,png,pdf等直接被浏览器解析了.在浏览器中显示其内容,没有下载. 下面通过网上查询得到一些解决方法: 最 ...

  4. 【数据下载】利用wget命令批量下载ftp文件和文件夹

    这是一个“”数据大发现”的时代,大家都在创造数据,使用数据以及分享数据,首先一步我们就需要从数据库download我们需要的数据. Ftp是一种常见的在线数据库,今天介绍一种可以批量下载文件夹的方法, ...

  5. wget 快速下载 ftp 文件

    GNU Wget 1.17.1,非交互式的网络文件下载工具. 用法: wget [选项]... [URL]... 长选项所必须的参数在使用短选项时也是必须的. 启动: -V, --version 显示 ...

  6. java实现ftp文件的上传与下载

    最近在做ftp文件的上传与下载,基于此,整理了一下资料.本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的... 1.首先引用3个包 ...

  7. C# 下载PDF文件(http与ftp)

    1.下载http模式的pdf文件(以ASP.NET为例,将PDF存在项目的目录下,可以通过http直接打开项目下的pdf文件) #region 调用本地文件使用返回pdfbyte数组 /// < ...

  8. java/struts/Servlet文件下载与ftp文件上传下载

    1.前端代码 使用超链接到Struts的Action或Servlet <a target="_blank" href="ftpFileAction!download ...

  9. java上传、下载、删除ftp文件

    一共三个类,一个工具类Ftputil.,一个实体类Kmconfig.一个测试类Test 下载地址:http://download.csdn.net/detail/myfmyfmyfmyf/669710 ...

随机推荐

  1. kubernetes in action - Volumes

    Volume解决Kubernetes的存储的问题 对于Pod使用的存储,抽象为volume,volume伴随着Pod的创建而创建,消失而同时消失,不能单独的创建 这样的好处,是存储的塑胶不会因为某个c ...

  2. qs.stringify和JSON.stringify()

    var a = {name:'hehe',age:10}; qs.stringify(a) // 'name=hehe&age=10' JSON.stringify(a) // '{" ...

  3. Typescript基础

    参数类型新特性: ​类型声明 变量/函数:类型(string.any.number.boolean.void五种基本类型) 例如:var age:number=19; function test(na ...

  4. mint-ui笔记

    1.安装: npm install mint-ui --save npm install babel-plugin-component --save-dev //(只引入部分组件时需要安装) 2.导入 ...

  5. canvas画圆角矩形的方法

    思路:arcTo(x1, y1, x2, y2, r) 参考:https://blog.csdn.net/shi851051279/article/details/80436851 http://ww ...

  6. 初尝Spring Cloud Config

    1,下载源码 地址https://spring.io/guides/gs/centralized-configuration/ 2,导入工程 解压后分别把Server端与Client端导入到两个Ecl ...

  7. JavaScript实现RSA加解密

    在GitHub上找到jsencrypt.js对RSA加解密的工具文件,地址分别是:https://github.com/travist/jsencrypt和https://github.com/ope ...

  8. iOS 上架注意

    一.推送证书 配置推送证书的流程说明:https://docs.aws.amazon.com/zh_cn/pinpoint/latest/developerguide/apns-setup.html ...

  9. Spring Boot入门 and Spring Boot与ActiveMQ整合

    1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...

  10. https://blog.csdn.net/uftjtt/article/details/79044186

    https://blog.csdn.net/uftjtt/article/details/79044186