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构建的,分好多 ...
随机推荐
- android.animation(6) - AnimatorSet
上几篇给大家分别讲了ValueAnimator和ObjectAnimator,相比而言ObjectAnimator更为方便而且由于set函数是在控件类内部实现,所以封装性更好.而且在现实使用中一般而言 ...
- swift 属性和方法
属性和常量 如果创建了一个结构体的实例并赋值给一个常量,则无法修改实例的任何属性: let rangeOfFourItems = FixedLengthRange(firstValue: 0, len ...
- jq判断元素class是否定义了CSS样式
if($("div").attr("class")==null) 如果$("div").attr("class")为空, ...
- 复现IIS6.0远程命令执行漏洞
出这个漏洞有一定时间了,一直没测试,因为知道这个漏洞有条件的,好吧,顺便感谢royal大佬今天晚上日内网的指点. 漏洞要求: 开启Webdav PS:不想刚拿下的内网服务器一下权限掉了,又不想放xx远 ...
- XML-RPC使用手册
内容列表 Preface: About This Manual Introduction to XML-RPC for C/C++ What is XML-RPC? How Does XML-RPC ...
- [uboot]Issue list
- Android——问题解决之adb not responding;adb不是内部或外部命令;path变量的默认值为多少
adb not responding 恩,这是出现的问题.我们开始来解决它吧! 出现这种问题大多是因为adb端口被占用导致这个问题,所以只要找到占用端口号程序,结束即可!就是这么简单(adb运行端口号 ...
- 《高性能MySQL》读书笔记(1)
慢查询 当一个资源变得效率低下的时候,应该了解一下为什么会这样.有如下可能原因:1.资源被过度使用,余量已经不足以正常工作.2.资源没有被正确配置3.资源已经损坏或者失灵 因为慢查询,太多查询的实践过 ...
- HTML5标准最终来了,看什么书学习最好??????
近期看了一本书<HTML5网页开发实例具体解释>,是大众点评的攻城狮写的,认为非常有收获.看样子眼下大多数的国内网页都支持HTML5了,全栈project师是不是必须得会HTML5? 有兴 ...
- 概率论 - BZOJ - 4001 TJOI2015
TJOI2015 Problem's Link ---------------------------------------------------------------------------- ...