在浏览器中从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. ...
随机推荐
- window/linux下获取文件MD5
MD5消息摘要算法(英语: MD5 Message-Digest Algorithm), 主要用于确保信息传输过程的一致性校验. 首先介绍两个工具: window: WinMD5Free Linu ...
- 汇编:汇编语言实现冒泡排序(loop指令实现)
;=============================== ;循环程序设计 ;loop指令实现 ;冒泡排序 ;for(int i=0;i<N;i++){ ; for(int h=0;j&l ...
- echarts饼图扇区添加点击事件
在echarts最后面添加上这段代码就可以了 function eConsole(param) { //alert(option.series[0].data.length); //alert(opt ...
- Linux 必会
一.一般命令:1.cd 进入磁盘文件夹2.ls- 查看当前文件夹包含哪些文件,注意-后面的3.pwd 立刻知道目前所在哪个文件及4.mkdir 创建文件夹5.touch touch命令用于修改文件或者 ...
- collections模块的使用
1. Counter counter是collections中的一个模块, 它能够统计出字符串/文本中的每一个元素出现的次数, 并可以对结果进行进一步的处理. 使用方法 传入: 字符串 默认返回: C ...
- STM32F4使用FPU+DSP库进行FFT运算的测试过程二
原文地址:http://www.cnblogs.com/NickQ/p/8541156.html 测试环境:单片机:STM32F407ZGT6 IDE:Keil5.20.0.0 固件库版本:STM32 ...
- vuls安装记录
第一步安装go环境apt-get install golang-go(显示出错,go版本apt安装太低,apt-get purge golang-go卸载后手动安装,必须1.8.3以上) 还需将/us ...
- 二维数组快速排序(sort+qsort)
二维数组快速排序 qsort是c中快速排序,如果简单的一维数组排序,想必大家的懂.现在看一下二维数组的排序,虽然可以冒泡但是太费时间了,我们这里使用qsort来快速排序,看代码应该看得懂吧. 代码: ...
- uva 509 RAID!(磁盘数据)
来自 https://blog.csdn.net/su_cicada/article/details/80085318 习题4-7 RAID技术(RAID!, ACM/ICPC World Final ...
- Jetson tx1 安装cuda错误
前两天安装Jetpack3.0的时候,看着网上的教程以为cuda会自动安装上,但是历经好几次安装,都安装不上cuda,也刷了好几次jetpack包.搜遍了网上的教程,也没有安装上.错误如下图所示: 这 ...