在浏览器中从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. ...
随机推荐
- bootstrap模态框传值操作
1.bootstrap模态框之html代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"& ...
- PyCharm入门第一步-——创建并运行第一个Python项目
创建项目 点击Create New Project 创建项目 输入自己的项目名,点击Create创建 创建文件 右键项目名创建python文件 创建一个HelloPython文件 输入print(&q ...
- android SearchView和ListView简单使用
其实我写代码最担心遇到关于适配器的使用,在我的感觉中适配器是个难度很大的知识点,但是不能因为难而不去学习啊,毕竟现在时间很充裕,可以慢慢学,所以,不会也要写,真所谓,迎难而上啊. 下面是Search ...
- jQuery(三)HTML
获得内容: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() - 设置或返回表单字段的值 <html> < ...
- ssh安装和使用
1.基础知识 ssh用于远程登陆,linux默认安装了client,如果需要被登陆则需要安装 server 2.安装 apt-get install openssh-server 检查是否安装成功 a ...
- 相亲数--Python
想亲数:在遥远的古代,人们发现某些自然数之间有特殊的关系:如果两个数a和b,a的所有除本身以外的因数之和等于b,b的所有除本身以外的因数之和等于a,则称a,b是一对相亲数 code: def sumF ...
- (数据科学学习手札12)K-means聚类实战(基于R)
上一篇我们详细介绍了普通的K-means聚类法在Python和R中各自的实现方法,本篇便以实际工作中遇到的数据集为例进行实战说明. 数据说明: 本次实战样本数据集来自浪潮集团提供的美团的商家信息,因涉 ...
- ABAP CDS ON HANA-(5)テーブル結合ビュー
JOINs in CDS View In ABAP CDS, Join between two data sources is allowed. Allowed joins are:- Inner J ...
- HBase 高级架构解析
整体框架 使用 ZooKeeper 框架协助 RegionServer(类似于HDFS的nodemanager)用户请求从 Client 到 Zookeeper 进行判断数据属于哪一个 Region ...
- set<pair<int,int> > 的运用
In this cafeteria, the N tables are all ordered in one line, where table number 1 is the closest to ...