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下载文件的更多相关文章

  1. C#FTP下载文件出现远程服务器返回错误: (500) 语法错误,无法识别命令

    如果下载多个文件的时候,有时候莫名其妙的出现500服务器错误,很有可能是没有设置KeepAlive 属性导致的. 出现应用程序未处理的异常:2015/1/6 11:40:56 异常类型:WebExce ...

  2. java实现FTP下载文件

    ftp上传下载文件,是遵照ftp协议上传下载文件的,本例仅以下载文件为例. 重要的方法解释: 1.FTP功能相关依赖路径:org.apache.commons.net.ftp.*: 2.ftp默认端口 ...

  3. .Net 连接FTP下载文件报错:System.InvalidOperationException: The requested FTP command is not supported when using HTTP proxy

    系统环境: Windows + .Net Framework 4.0   问题描述: C#连接FTP下载文件时,在部分电脑上有异常报错,在一部分电脑上是正常的:异常报错的信息:System.Inval ...

  4. c#.net从ftp下载文件到本地

    c#.net从ftp下载文件到本地      /*首先从配置文件读取ftp的登录信息*/  ;                     ;                     , buffer_c ...

  5. android中使用Http下载文件并保存到本地SD卡

    1.AndroidMainfest.xml中设置权限 <uses-permission android:name="android.permission.INTERNET"& ...

  6. python从FTP下载文件

    #!/usr/bin/python # -*- coding: utf-8 -*- """ FTP常用操作 """ from ftplib ...

  7. FTP下载文件失败

    这几天的定时任务下载文件的脚本失败了. 于是手工执行测试,发现报550 Permission denied. Passive mode refused. 意思就是被动模式下,没有权限获取文件. 解决方 ...

  8. python_ftplib实现通过FTP下载文件

    1.  Ftplib常用函数介绍 Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,本次主要介绍连接FTP并且进行文件下载功能,可 ...

  9. 远程FTP下载文件

    现在存在以下环境: 远程服务器:192.168.1.107 用户名:dt 密码:dt123 需要从该服务器上下载文件到本地 1.登录(进入到那个目录登录的 ,文件就会被下载到该文件) ftp 192. ...

随机推荐

  1. 修改pytorch官方实例适用于自己的二分类迁移学习项目

    本demo从pytorch官方的迁移学习示例修改而来,增加了以下功能: 根据AUC来迭代最优参数: 五折交叉验证: 输出验证集错误分类图片: 输出分类报告并保存AUC结果图片. import os i ...

  2. NAND Flash结构及驱动函数

    目标:以NAND Flash K9F2G08U0M为例介绍其结构及其驱动程序的书写 1. 结构 由芯片手册中的图可知:K9F2G08U0M大小为2112Mbits(即 256MB = 2Gb ) 共有 ...

  3. MetInfo最新网站漏洞如何修复以及网站安全防护

    metinfo漏洞于2018年10月20号被爆出存在sql注入漏洞,可以直接拿到网站管理员的权限,网站漏洞影响范围较广,包括目前最新的metinfo版本都会受到该漏洞的攻击,该metinfo漏洞产生的 ...

  4. PHP教程专题资源免费下载地址收藏

     PHP教程专题资源免费下载地址收藏 PHP,即Hypertext Preprocessor,是一种被广泛应用的开源通用脚本语言,尤其适用于 Web 开发并可嵌入 HTML 中去.它的语法利用了 C. ...

  5. 相亲数--Python

    想亲数:在遥远的古代,人们发现某些自然数之间有特殊的关系:如果两个数a和b,a的所有除本身以外的因数之和等于b,b的所有除本身以外的因数之和等于a,则称a,b是一对相亲数 code: def sumF ...

  6. go学习笔记-语言指针

    语言指针 定义及使用 变量是一种使用方便的占位符,用于引用计算机内存地址.取地址符是 &,放到一个变量前使用就会返回相应变量的内存地址. 一个指针变量指向了一个值的内存地址.类似于变量和常量, ...

  7. SQL Server中2008及以上 ldf 文件过大的解决方法

    在SQL Server中经常遇到事务日志变大的情况,除了将数据库设置为“自动收缩”外,还可以使用下面的SQL命令进行快速清除数据库中的事务日志,命令如下:  - 第一步:清空日志  ALTER DAT ...

  8. JS控制文本框输入的内容

    总而言之:   先在‘<input>’ 里输入      onkeyup="value=value.replace(/[^\X]/g,'')" 然后在(/[\X]/g, ...

  9. python第三天(dictionary应用)转

    1.题目: python实现英文文章中出现单词频率的统计   前言: 这道题在实际应用场景中使用比较广泛,比如统计历年来四六级考试中出现的高频词汇,记得李笑来就利用他的编程技能出版过一本背单词的畅销书 ...

  10. Visual Studio 6.0安装包

    点击下载