【转】C#的FTP上传函数问题
来源:互联网 发布:mac office账号注销 编辑:程序博客网 时间:2024/06/04 20:14
转自:http://bbs.csdn.net/topics/330260539
FTP上传函数如下,可以确定代码无错
internal bool Upload(string fileName, string uploadUrl,string UserName,string Password)
{//fileName上傳的檔案ex : c:\abc.xml , uploadUrl上傳的FTP伺服器路徑ftp://127.0.0.1,UserName使用者FTP登入帳號 , Password使用者登入密碼
Stream requestStream = null;
FileStream fileStream = null;
FtpWebResponse uploadResponse = null;
try
{
FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uploadUrl);
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;//設定Method上傳檔案
uploadRequest.Proxy = null;
if (UserName.Length > 0)//如果需要帳號登入
{
NetworkCredential nc = new NetworkCredential(UserName, Password);
uploadRequest.Credentials = nc; //設定帳號
}
requestStream = uploadRequest.GetRequestStream();
fileStream = File.Open(fileName, FileMode.Open);
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{//開始上傳資料流
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Close();
uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
}
现有一FTP服务器IP:127.0.0.1 用户名:admin 密码:admin 要实现上传功能该如何?我帖出我的代码,请大家帮忙指正
private void button_Click(object sender, EventArgs e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "C:\\";
openFileDialog1.Filter = "xls files (*.xls)|*.xls|doc files (*.doc)|*.doc";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if ((myStream = openFileDialog1.OpenFile()) != null)
{
Upload(openFileDialog1.FileName, "ftp://127.0.0.1", "admin", "admin");
myStream.Close();
}
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- 【转】C#的FTP上传函数问题
- 【转】C# 上传本地文件至ftp上的方法
- c#下winform的ftp上传实验
- c#下winform的ftp上传
- C#下的FTP上传和下载
- C# FTP上传代码
- c# FTP文件上传
- C#上传FTP服务器
- C# FTP上传的实现方法 (包括断点上传)
- FTP上传问题总结
- C# FTP文件上传类
- C#上传文件到ftp
- C# ftp 上传、下载、删除
- C# ftp上传和下载
- C# ftp 上传、下载、删除
- C# 上传本地文件至ftp上的方法
- 【FTP】ftp上传文件到AIX遇到ftp 450错误的问题
- 【转】C# FTP上传文件至服务器代码
- C++语言处理异常
- 习题2-8
- MySQL查询缓存
- libuv库定时器的使用
- 10个顶级Web移动开发JavaScript框架
- 【转】C#的FTP上传函数问题
- Permission is only granted to system apps
- 第五章作业
- hdu 1075 What Are You Talking About 字典树(静态版)
- 【编程小练习】删除字符串中的子串
- Spring MVC 教程,快速入门,深入分析
- UESTC 838 —— 母仪天下(树状数组模板)
- POJ 1573 Robot Motion
- C++运算符重载(一)——加减