利用下载一段代码下载ftp服务器上的文件:
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(fileFullPath));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(userName, userPwd);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); System.IO.Stream ftpStream = response.GetResponseStream(); int bufferSize = 1024 * 5;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));
while (readCount > 0)
{
Response.OutputStream.Write(buffer, 0, readCount);//从缓冲区读取的数据,写入http流中,
Response.Flush(); //发送到客户端 向客户端发送出当前缓冲的输出
readCount = ftpStream.Read(buffer, 0, bufferSize);//再读取数据流中的数据
}
ftpStream.Close();
response.Close();
下载之后的文件自动添加了网页源码文件面容如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
Untitled Page
</title><link href="../Css/dinwei.css" rel="stylesheet" type="text/css" /> <script language="javascript" src="../js/ValidateInputChar.js"></script> <script language="javascript" src="../js/dateTwo.js"></script> <script type="text/javascript">
function CheckAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
if (e.name != 'chkall')
e.checked = form.chkall.checked;
}
}
function NoCheckAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
if (e.name != 'chkall')
e.checked = form.chkall.checked;
}
}
</script></head>
<body>
<form name="form1" method="post" action="MaterialManage.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
</html>上面只是简写的有一部分内容没有贴出来.你用代码试试就知道了.
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(fileFullPath));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(userName, userPwd);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); System.IO.Stream ftpStream = response.GetResponseStream(); int bufferSize = 1024 * 5;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));
while (readCount > 0)
{
Response.OutputStream.Write(buffer, 0, readCount);//从缓冲区读取的数据,写入http流中,
Response.Flush(); //发送到客户端 向客户端发送出当前缓冲的输出
readCount = ftpStream.Read(buffer, 0, bufferSize);//再读取数据流中的数据
}
ftpStream.Close();
response.Close();
下载之后的文件自动添加了网页源码文件面容如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
Untitled Page
</title><link href="../Css/dinwei.css" rel="stylesheet" type="text/css" /> <script language="javascript" src="../js/ValidateInputChar.js"></script> <script language="javascript" src="../js/dateTwo.js"></script> <script type="text/javascript">
function CheckAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
if (e.name != 'chkall')
e.checked = form.chkall.checked;
}
}
function NoCheckAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
if (e.name != 'chkall')
e.checked = form.chkall.checked;
}
}
</script></head>
<body>
<form name="form1" method="post" action="MaterialManage.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
</html>上面只是简写的有一部分内容没有贴出来.你用代码试试就知道了.
Response.Close();
这样就可以了.