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构建的,分好多 ...
随机推荐
- Atitit.软件仪表盘(8)--os子系统--资源占用监测
Atitit.软件仪表盘(8)--os子系统--资源占用监测 CPU使用 内存使用 磁盘队列 任务管理器 网络速度 插件列表( 资源管理器插件,浏览器插件,360optim) 启动项管理 (350) ...
- 【Tips】【UE】总结自己常用的UltraEdit使用技巧
如果您问我每天都要打开的软件是什么,那毫无疑问是UltraEdit!作为一位DBA,每天都要写各种脚本,尤其是在对具有超多行行的大文件进行精心编辑时,没有一个好的文本编辑器是不成的.掐指一算,哇塞,自 ...
- JsonHelper修改4.0
public class JsonHelper { /// <summary> /// 将对象序列化为JSON格式 /// </summary> /// <param n ...
- -[__NSArrayI removeAllObjects]: unrecognized selector sent to instance 0x7fa8dc830110
问题 今天做项目,遇到了这个问题 -[__NSArrayI removeAllObjects]: unrecognized selector sent to instance 0x7fa8dc8301 ...
- 《LAMP系统工程师实用教程》读书笔记(一)- linux常用命令
图书馆看到就借回来啦,这本书虽然其貌不扬,对新手还是挺棒的.来学下shell脚本和php. 把书上不熟悉的命令和选项记录下. mkdir -v创建文件夹并返回信息 mkdir -m创建文件夹并设定权限 ...
- linux 无外网情况下安装 mysql
由于工作需要,需要在一台装有 CentOS 系统的测试服务器上安装 MySQL ,由于该服务器上存有其他比较重要的测试数据,所以不能连接外网.由于之前安装 MySQL 一直都是使用 yum 命令一键搞 ...
- hdu1142(dj+记忆化搜索)
题意:给你n各点,m行关于这些点的联通关系,以及距离,求从1这个点到2这个点之间,下一个点到2这个点比当前点到2这个点的距离要小的路径的条数...... 思路:dj+记忆化搜索....... #inc ...
- VBA 获得绝对地址控制焦点的设置
先上代码,有时间再补上说明. Dim Mefoucs As String MsgBox "你选定的当前单元格是:" & Selection.Address Mefoucs ...
- fastjson常用操作
一. fastjson生成json字符串(JavaBean,List<JavaBean>,List<String>,List<Map<String,Object&g ...
- TensorFlow基础笔记(4) Tensor Transformation
https://segmentfault.com/a/1190000008793389 抽取 tf.slice(input_, begin, size, name=None):按照指定的下标范围抽取连 ...