在浏览器中从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. ...
随机推荐
- C#提取html中的汉字
using System.Text.RegularExpressions; private string StripHT(string strHtml) //从html中提取纯文本 { Regex r ...
- 利用百度地图API实现地址和经纬度互换查询
import json import requests def baiduMap(input_para): headers = { 'User-Agent': 'Mozilla/5.0 (Window ...
- 字符编码 ASCII unicode UTF-8
字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(b ...
- sublime3常用插件总结
本人之前使用的是webstorm,后来改用sublime,渐渐的爱上了它的快!(自行体会) 正式介绍sublime3常用的一些插件,安装流程不再赘述! SublimeTmpl 创建常用文件初始模板,必 ...
- doc命令操作数据库(下)
1.给数据表添加一组数据: 2.给数据表添加多组数据: 3.对数据进行删除和修改: 4.用select查询单个或多个数据信息: 5.去除重复值: 6.查询的各种用法: between的用法: 查询排序 ...
- hadoop生态搭建(3节点)-03.zookeeper配置
# https://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html # ===== ...
- 8.2 USB键盘驱动编写和测试
目标:根据USB驱动分析和上节的USB鼠标驱动,编写键盘驱动,并测试. 一.原理分析 1. 首先通过打印usb_buf[i]中的8字节数据,看一下按键按下之后会接收到什么. 1)通过按完所有键盘按键打 ...
- python三大神器之生成器
生成器Generator: 本质:迭代器(所以自带了__iter__方法和__next__方法,不需要我们去实现) 特点:惰性运算,开发者自定义 在python中有三种方法来获取生成器: 1.通过生成 ...
- ruby中将数字转化为字符串格式时差
工作中有时候会碰到需要把数值展示成比较直观的时间差格式,divmod方法很适合做这个操作. divmod #输出商和余数的数组 60.divmod(50) #=> [1, 10 ...
- 005---基于UDP的套接字
基于UDP的套接字 udp不同于tcp协议:不需要经过三次握手.四次挥手.直接发送数据就行. 服务端 import socket ip_port = ('127.0.0.1', 8001) buffe ...