Web浏览器导出FTP服务器上的文件
开发思路:
1.代码登录ftp服务器下载文件到服务器
2.通过web浏览器下载服务器上的文件 using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
public partial class Test : System.Web.UI.Page
{
public string FtpFilePath
{
get { return Request["filepath"]; }
}
private string _FtpLoginUserName = "test";
private string _FtpLoginPassword = "123456";
protected void Page_Load(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(FtpFilePath))
{
Response.Write("缺少重要参数:文件路径");
Response.End();
}
else
{
//Download("ftp://192.168.0.130/Attend_Data/2015/KQ_DETAIL_201504.csv");
Download(FtpFilePath);
}
} #region FTP服务器下载文件
/// <summary>
/// FTP服务器下载文件
/// </summary>
/// <param name="filePath"></param>
/// <param name="fileName"></param>
private void Download(string ftpFilePath)
{
FtpWebRequest reqFTP;
try
{
string filePath = AppDomain.CurrentDomain.BaseDirectory + "temp\\AttendanceDataExport\\";// + "fileName.csv";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
string ftpFileNmae = "";
ftpFileNmae = ftpFilePath.Substring(ftpFilePath.LastIndexOf('/') + );
if (string.IsNullOrEmpty(ftpFileNmae))
{
ftpFileNmae = "KQ_DETAIL_" + DateTime.Now.Ticks + ".csv";
}
string fileName = filePath + ftpFileNmae;
if (File.Exists(fileName))
{
try
{
File.Delete(fileName);
}
catch (Exception ex1)
{ }
}
FileStream outputStream = new FileStream(fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFilePath));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(_FtpLoginUserName, _FtpLoginPassword); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = ;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, , bufferSize);
while (readCount > )
{
outputStream.Write(buffer, , readCount);
readCount = ftpStream.Read(buffer, , bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close(); ResponseExcel(fileName); }
catch (System.Threading.ThreadAbortException) { }
catch (Exception ex)
{
Response.Write("Download error" + ex.Message);
}
} /// <summary>
/// 浏览器导出
/// </summary>
/// <param name="fileName"></param>
private void ResponseExcel(string fileName)
{
try
{
System.IO.FileInfo file = new System.IO.FileInfo(fileName);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
if (Page.Request.Browser.Browser == "IE")
{
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpContext.Current.Server.UrlEncode(file.Name));
}
else
{
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
}
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载
HttpContext.Current.Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端
HttpContext.Current.Response.WriteFile(file.FullName);
// 停止页面的执行 HttpContext.Current.Response.End();
}
catch (System.Threading.ThreadAbortException) { }
catch (Exception ex)
{
this.Response.Write(ex.Source + ex.Message + ex.StackTrace);
}
}
#endregion
}
Web浏览器导出FTP服务器上的文件的更多相关文章
- 浏览器通过http协议通过nginx访问ftp服务器上的文件
1.修改nginx配置文件 2.修改nginx默认目录为ftp默认目录 3.修改成自定义路径后,并修改此目录第一行,修改为root或是nginx用户 4.重新加载 cd /usr/local/ngin ...
- “打开ftp服务器上的文件夹时发生错误,请检查是否有权限访问该文件夹"
阿里云虚拟主机上传网站程序 问题场景:网页制作完成后,程序需上传至虚拟主机 注意事项: 1.Windows系统的主机请将全部网页文件直接上传到FTP根目录,即 / . 2. 如果网页文件较多,上传较慢 ...
- 打开FTP服务器上的文件夹时发生错误,请检查是否有权限访问该文件夹
打开FTP服务器上的文件夹时发生错误,请检查是否有权限访问 在win98,winme,win2000,win2003下都能正常上传文件夹,但在winxp+sp2下同样的文件夹就可能出现问题 1. 打开 ...
- FTP服务器上删除文件夹失败
很多人都知道:要删除FTP服务器上的文件夹时,必须确保文件夹下面没有其他文件,否则会删除失败! 可是,有些服务器考虑到安全等因素,通常会隐藏以点开始的文件名,例如“.test.txt”.于是,有的坏人 ...
- JAVA通过FTP方式向远程服务器或者客户端上传、下载文件,以及删除FTP服务器上的文件
1.在目标服务器上搭建FTP服务器 搭建方式有多种大家可以自行选择,例如使用Serv-U或者FTPServer.exe:这里我以FTPServer.exe为例搭建:在目标服务器(这里对应的IP是10. ...
- 【FTP】C# System.Net.FtpClient库连接ftp服务器(上传文件)
如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...
- windows server 打开 FTP 服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。
解决方案1: 打开高级安全windows防火墙,设置出入站规则. 然后,再打开windows防火墙界面,点击左上角“允许程序或功能通过windows防火墙”,勾选上设置的出入站名称和FTP服务器. 如 ...
- Python向FTP服务器上传文件
上传 代码示例: #!/usr/bin/python # -*- coding:utf-8 -*- from ftplib import FTP ftp = FTP() # 打开调试级别2, 显示详细 ...
- 使用SAXReader读取ftp服务器上的xml文件(原创)
根据项目需求,需要监测ftp服务器上的文件变化情况,并将新添加的文件读入项目系统(不需要下载). spring配置定时任务就不多说了,需要注意的一点就是,现在的项目很多都是通过maven构建的,分好多 ...
随机推荐
- Error-Project facet Java version 1.8 is not supported
最近导入最新的Strtus2.5.10.1 Demo时出现了这个错误 解决方案如下: 选中工程——右键——Properties 然后依次展开找到如图所示内容,将1.8改成1.7即可. 原因:工程默认配 ...
- centos系统字符编码问题
1.cat /etc/locale.conf LANG="en_US.UTF-8" 2./etc/profile.d/lang.sh这个文件要有且可以正常执行(检查/sbin/co ...
- mysqlbinlog 导出某时间段的是二进制日志
mysqlbinlog --no-defaults --start-datetime="2016-07-26 00:00:00" --stop-datetime="201 ...
- Webservice实现与调用(基于Spring的多种方式)
一.Webservice实现 实现方式分类 1. spring实现 bean配置 <bean class="org.springframework.remoting.jaxws.Sim ...
- django 文件上传(阿里云oss)下载(支持大文件下载)
1.文件上传 Models 设计 class Upload_File(models.Model): image = models.FileField(upload_to='file/%Y/%m',de ...
- [dts]TI-am437x dts
imx6 可以参考http://blog.csdn.net/shengzhadon/article/details/49908439 参照文件: Documentation/devicetree/bi ...
- strerror和perror函数详解
/*#include <string.h> char *strerror(int errnum); 它返回errnum的值所对应的错误提示信息,例如errnum等于12的话,它就会返回&q ...
- Android基础总结(九)多媒体
多媒体概念(了解) 文字.图片.音频.视频 计算机图片大小的计算(掌握) 图片大小 = 图片的总像素 * 每个像素占用的大小 单色图:每个像素占用1/8个字节 16色图:每个像素占用1/2个字节 25 ...
- 哪一个不是EL定义的隐式对象?(选择1项)
哪一个不是EL定义的隐式对象?(选择1项) A cookie B.pageContext C.attributes D initParam 解答:C 1)pageContext:JSP 页的上下文.它 ...
- Java设计模式菜鸟系列(十)模板方法模式建模与实现
转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39806973 模板方法模式(Template Method):在一个方法中定义了一个算法的 ...