c#.net ftp链接
问大家一个问题,我在界面上做了一个LinkButton1,代码是以下protectedvoidLinkButton1_Click(objectsender,EventAr...
问大家一个问题,我在界面上做了一个LinkButton1,代码是以下
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("ftp://*服务器名称*/");
}
但是点之后跳转不了,我的问题就是 FTP服务器怎么在IE上实现跳转或者点击之后打开个explorer提示输入用户名密码的,
*服务器名称* 是存在的。
小菜鸟,求指教 展开
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("ftp://*服务器名称*/");
}
但是点之后跳转不了,我的问题就是 FTP服务器怎么在IE上实现跳转或者点击之后打开个explorer提示输入用户名密码的,
*服务器名称* 是存在的。
小菜鸟,求指教 展开
1个回答
展开全部
using System.Net;
private void btnOpen_Click(object sender, EventArgs e)
{
Cursor currentCursor = this.Cursor;
FtpWebResponse response = null;
Stream stream = null;
try
{
this.Cursor = Cursors.WaitCursor;
// Create the FtpWebRequest object.
//第一步,生成FtpWebRequest对象
Uri baseUri = new Uri(txtServer.Text);//txtServer输入形如ftp://server的字符串
FtpWebRequest request =(FtpWebRequest)WebRequest.Create(baseUri);
//第二步,设置访问服务器的用户名和密码
request.Credentials = new NetworkCredential(txtUsername.Text,
txtPassword.Text);//访问Ftp服务器的用户名和密码
//第三步,设置访问服务器的方法
request.Method = WebRequestMethods.Ftp.ListDirectory; //访问ftp服务器的方法,
// Send the request to the server.
//第四步,将request发送到服务器
//将请求发送到ftp服务器
response = (FtpWebResponse)request.GetResponse();
//第五步,获取服务器的response
// Read the response and fill the list box.
stream = response.GetResponseStream();
//第六步,对从服务器获取的stream进行处理
Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
FillDirectoryList(stream,encode);
serverDirectory = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error FTP Client",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//第七步,将使用的response,stream等对象关闭
finally
{
if (response != null)
response.Close();
if (stream != null)
stream.Close();
this.Cursor = currentCursor;
}
}
///对从服务器获取的stream进行处理
private void FillDirectoryList(Stream stream,Encoding encode)
{
StreamReader reader = new StreamReader(stream,encode);
string content = reader.ReadToEnd();
string[] files = content.Split('\n');
lstFiles.DataSource = files;
reader.Close();
}
//从ftp服务器下载文件
private void btnGetFile_Click(object sender, EventArgs e)
{
FtpWebResponse response = null;
Stream inStream = null;
Stream outStream = null;
try
{
Uri baseUri = new Uri(txtServer.Text);
string filename = lstFiles.SelectedValue.ToString().Trim();
string fullFilename = serverDirectory + @"/" + filename;
Uri uri = new Uri(baseUri, fullFilename);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(txtUsername.Text,
txtPassword.Text);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.UseBinary = checkBoxBinary.Checked;
response = (FtpWebResponse)request.GetResponse();
inStream = response.GetResponseStream();
saveFileDialog.FileName = filename;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
outStream = File.OpenWrite(saveFileDialog.FileName);
byte[] buffer = new byte[4096];
int size = 0;
while ((size = inStream.Read(buffer, 0, 4096)) > 0)
{
outStream.Write(buffer, 0, size);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error FTP Client",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (inStream != null)
inStream.Close();
if (outStream != null)
outStream.Close();
if (response != null)
response.Close();
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
Cursor currentCursor = this.Cursor;
FtpWebResponse response = null;
Stream stream = null;
try
{
this.Cursor = Cursors.WaitCursor;
// Create the FtpWebRequest object.
//第一步,生成FtpWebRequest对象
Uri baseUri = new Uri(txtServer.Text);//txtServer输入形如ftp://server的字符串
FtpWebRequest request =(FtpWebRequest)WebRequest.Create(baseUri);
//第二步,设置访问服务器的用户名和密码
request.Credentials = new NetworkCredential(txtUsername.Text,
txtPassword.Text);//访问Ftp服务器的用户名和密码
//第三步,设置访问服务器的方法
request.Method = WebRequestMethods.Ftp.ListDirectory; //访问ftp服务器的方法,
// Send the request to the server.
//第四步,将request发送到服务器
//将请求发送到ftp服务器
response = (FtpWebResponse)request.GetResponse();
//第五步,获取服务器的response
// Read the response and fill the list box.
stream = response.GetResponseStream();
//第六步,对从服务器获取的stream进行处理
Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
FillDirectoryList(stream,encode);
serverDirectory = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error FTP Client",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//第七步,将使用的response,stream等对象关闭
finally
{
if (response != null)
response.Close();
if (stream != null)
stream.Close();
this.Cursor = currentCursor;
}
}
///对从服务器获取的stream进行处理
private void FillDirectoryList(Stream stream,Encoding encode)
{
StreamReader reader = new StreamReader(stream,encode);
string content = reader.ReadToEnd();
string[] files = content.Split('\n');
lstFiles.DataSource = files;
reader.Close();
}
//从ftp服务器下载文件
private void btnGetFile_Click(object sender, EventArgs e)
{
FtpWebResponse response = null;
Stream inStream = null;
Stream outStream = null;
try
{
Uri baseUri = new Uri(txtServer.Text);
string filename = lstFiles.SelectedValue.ToString().Trim();
string fullFilename = serverDirectory + @"/" + filename;
Uri uri = new Uri(baseUri, fullFilename);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(txtUsername.Text,
txtPassword.Text);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.UseBinary = checkBoxBinary.Checked;
response = (FtpWebResponse)request.GetResponse();
inStream = response.GetResponseStream();
saveFileDialog.FileName = filename;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
outStream = File.OpenWrite(saveFileDialog.FileName);
byte[] buffer = new byte[4096];
int size = 0;
while ((size = inStream.Read(buffer, 0, 4096)) > 0)
{
outStream.Write(buffer, 0, size);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error FTP Client",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (inStream != null)
inStream.Close();
if (outStream != null)
outStream.Close();
if (response != null)
response.Close();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |