在浏览器中从FTP下载文件
public static class FTPHelper
{
/// <summary>
/// 得到特定FTP目录的文件列表
/// </summary>
/// <param name="uri">ftp://{username}:{password}@ftp.baidu.com/{folderName}</param>
public static List<String> ListFiles(Uri serverUri)
{
// The serverUri parameter should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
throw new ArgumentException("uri must be ftp scheme");
} FtpWebRequest ftpRequest = null;
StreamReader reader = null;
try
{
// Get the object used to communicate with the server.
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(serverUri); ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; reader = new StreamReader(ftpRequest.GetResponse().GetResponseStream(), Encoding.Default); //read data.
List<String> fileNames = new List<String>();
string line = reader.ReadLine();
while (line != null)
{ fileNames.Add(line);
line = reader.ReadLine();
} return fileNames; }
catch(Exception ex)
{
throw ex;
}
finally
{
if(reader != null)
{
reader.Close();
}
if (ftpRequest != null)
{
ftpRequest.Abort();
}
}
} /// <summary>
/// 得到特定文件的大小
/// </summary>
/// <param name="uri">ftp://{username}:{password}@ftp.baidu.com/{fileName}</param>
public static long GetFileSize(Uri serverUri)
{
// The serverUri parameter should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
throw new ArgumentException("uri must be ftp scheme");
} FtpWebRequest ftpRequest = null;
StreamReader reader = null;
try
{
// Get the object used to communicate with the server.
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(serverUri); ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize; return ftpRequest.GetResponse().ContentLength;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (reader != null)
{
reader.Close();
}
if (ftpRequest != null)
{
ftpRequest.Abort();
}
}
}
}
调用ListFile方法把FTP特定目录的所有文件列表显示在web页面上,当单击名称时,下载文件
/// <summary>
/// 下载文件(此方法定义在实现Controller某类的某个MVC Controller中)
/// </summary>
/// <param name="uri"></param>
public void DownLoadFile(Uri uri,string fileName)
{
//Create a stream for the file
Stream stream = null; //This controls how many bytes to read at a time and send to the client
int bytesToRead = ; // Buffer to read bytes in chunk size specified above
byte[] buffer = new Byte[bytesToRead]; // The number of bytes read
try
{
long fileSize = FTPHelper.GetFileSize(uri);
//Create a WebRequest to get the file
FtpWebRequest fileReq = (FtpWebRequest)FtpWebRequest.Create(uri); //Create a response for this request
FtpWebResponse fileResp = (FtpWebResponse)fileReq.GetResponse(); //Get the Stream returned from the response
stream = fileResp.GetResponseStream(); // prepare the response to the client. resp is the client Response
var resp = HttpContext.Response; //Indicate the type of data being sent
resp.ContentType = "application/octet-stream"; //Name the file
resp.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
resp.AddHeader("Content-Length", fileSize.ToString()); int length;
do
{
// Verify that the client is connected.
if (resp.IsClientConnected)
{
// Read data into the buffer.
length = stream.Read(buffer, , bytesToRead); // and write it out to the response's output stream
resp.OutputStream.Write(buffer, , length); // Flush the data
resp.Flush(); //Clear the buffer
buffer = new Byte[bytesToRead];
}
else
{
// cancel the download if client has disconnected
length = -;
}
} while (length > ); //Repeat until no data is read
}
finally
{
if (stream != null)
{
//Close the input stream
stream.Close();
}
}
}
在浏览器中从FTP下载文件的更多相关文章
- C#FTP下载文件出现远程服务器返回错误: (500) 语法错误,无法识别命令
如果下载多个文件的时候,有时候莫名其妙的出现500服务器错误,很有可能是没有设置KeepAlive 属性导致的. 出现应用程序未处理的异常:2015/1/6 11:40:56 异常类型:WebExce ...
- java实现FTP下载文件
ftp上传下载文件,是遵照ftp协议上传下载文件的,本例仅以下载文件为例. 重要的方法解释: 1.FTP功能相关依赖路径:org.apache.commons.net.ftp.*: 2.ftp默认端口 ...
- .Net 连接FTP下载文件报错:System.InvalidOperationException: The requested FTP command is not supported when using HTTP proxy
系统环境: Windows + .Net Framework 4.0 问题描述: C#连接FTP下载文件时,在部分电脑上有异常报错,在一部分电脑上是正常的:异常报错的信息:System.Inval ...
- c#.net从ftp下载文件到本地
c#.net从ftp下载文件到本地 /*首先从配置文件读取ftp的登录信息*/ ; ; , buffer_c ...
- android中使用Http下载文件并保存到本地SD卡
1.AndroidMainfest.xml中设置权限 <uses-permission android:name="android.permission.INTERNET"& ...
- python从FTP下载文件
#!/usr/bin/python # -*- coding: utf-8 -*- """ FTP常用操作 """ from ftplib ...
- FTP下载文件失败
这几天的定时任务下载文件的脚本失败了. 于是手工执行测试,发现报550 Permission denied. Passive mode refused. 意思就是被动模式下,没有权限获取文件. 解决方 ...
- python_ftplib实现通过FTP下载文件
1. Ftplib常用函数介绍 Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,本次主要介绍连接FTP并且进行文件下载功能,可 ...
- 远程FTP下载文件
现在存在以下环境: 远程服务器:192.168.1.107 用户名:dt 密码:dt123 需要从该服务器上下载文件到本地 1.登录(进入到那个目录登录的 ,文件就会被下载到该文件) ftp 192. ...
随机推荐
- 洛谷P3804 【模板】后缀自动机
题目描述 给定一个只包含小写字母的字符串 SS , 请你求出 SS 的所有出现次数不为 11 的子串的出现次数乘上该子串长度的最大值. 输入输出格式 输入格式: 一行一个仅包含小写字母的字符串 SS ...
- gcc 手动安装
由于公司内部服务器没有联通外网,只能苦逼的手动安装gcc(自带的版本太老) rpm -ivh ppl-0.10.2-11.el6.x86_64.rpm rpm -ivh cloog-ppl-0.15. ...
- ELK初学搭建
目录:基础准备 修改相关系统配置 安装elasticsearch 安装 kibana 安装logstash X-pack插件的安装 登录网页查看 ELK名字解释 ELK就是ElasticSearch ...
- intellij IEDA 从svn拉环境到正常运行
intellij IEDA 从svn拉环境到正常运行 1.svn拉项目 在项目选择界面点击Check out from Version Control 从中选择Subversion(SVN) 2.选 ...
- 【redis常用的键值操作及性能优化】
服务端 启动redis服务 { // -a:指定密码 -h:指定主机 -p:指定端口 } //让redis 服务中断崩溃 //保存和关闭 //后台备份 //设置登录密码 //redis-benchma ...
- vue 导出流文件excel
第一种方法:需要设置响应类型,这里还需要安装 npm install js-file-download --save ,然后引用 var fileDownload = require('js-file ...
- Angular : 基础语句说明, 响应式表单指令, 组件生命周期钩子
- CentOS下禁止防火墙
CentOS下禁止防火墙 1.使用如下命令安装iptables-services. yum install -y iptables-services 2.关闭防火墙. service iptables ...
- IT类职位常用缩写 SA SD RD PG PM DBA MIS QA Sales
身为IT民工的基本常识,IT类职位常用缩写 SA (System Analyst) 系统分析师 在软体开发团队中,属于中高阶的基层管理者与领导者.除了须具备优秀的文字.语言沟通能力之外,还要有良好的分 ...
- 我的机器学习之路--anaconda环境搭载
网上许多教程比较晦涩难懂,本教程按照笔者(新手)自己的视角记录,希望给大家一些帮助 1.安装anaconda 目前比较推荐的机器学习环境为anaconda. Anaconda指的是一个开源的Pytho ...