|
调用页面:先放一个FileUpload控件:
string filename = chdFtp.UniqueName(FileUpload1.FileName);chdFtp one = new chdFtp(filename, FileUpload1.PostedFile.InputStream); //Fileupload的文件PostedFile的返回流InputStreambool ftpUpdateFlag = one.UploadVideo();
类库:
using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;using System.IO;using System.Threading;namespace CHDFTP{/*Coder:MarkTime:2011.04.28描述:实现FTP上传,删除,主要用到两个Stream流,_localfile获得上传文件的对象,_uploadstream创建FTP上传流,对应FtpWebRequest.GetRequestStream();实际操作是uploadRequest.GetResponse()方法;*/public class chdFtp{const int _bufferLength = 2048; //2K缓冲区byte[] Buffer = new byte[_bufferLength];int contentLen = 0;private string _ftpserverip = ConfigurationManager.AppSettings["ServerIP"].ToString();private string _ftpvideoid = ConfigurationManager.AppSettings["FtpID"].ToString();private string _ftpvideopwd = ConfigurationManager.AppSettings["FtpPwd"].ToString();private string _fileName;private FtpWebRequest uploadRequest = null;private FtpWebResponse uploadRespose = null;Stream _localfile = null;Stream _uploadstream = null;public chdFtp(string fileName){this._fileName = fileName;}public chdFtp(string fileName,Stream fs){this._fileName = fileName;this._localfile = fs;}public bool UploadVideo(){uploadRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(string.Format(@"ftp://{0}/{1}", _ftpserverip, _fileName))); //设定上传文件的路径与文件名,默认是FTP根目录uploadRequest.Credentials = new NetworkCredential(_ftpvideoid, _ftpvideopwd); //提供账号密码的验证uploadRequest.KeepAlive = false; //默认为true是上传完后不会关闭FTP连接uploadRequest.ReadWriteTimeout = 300000; //默认300000ms,5分钟uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;uploadRequest.UseBinary = true; //默认true,表示传输文件是二进制流,false表示是文本文档。try{_uploadstream = uploadRequest.GetRequestStream();do{contentLen = _localfile.Read(Buffer, 0, _bufferLength);_uploadstream.Write(Buffer, 0, contentLen);} while (contentLen != 0);_uploadstream.Close(); //必须先关闭stream流才可以进行上传。uploadRespose = (FtpWebResponse)uploadRequest.GetResponse();}catch (UriFormatException ex){return false;}catch (IOException ex){return false;}catch (WebException ex){return false;}finally{if (uploadRespose != null)uploadRespose.Close();if (_localfile != null)_localfile.Close();if (_uploadstream != null)_uploadstream.Close();}return true; }public bool DeleteVideo(){uploadRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(string.Format(@"ftp://{0}/{1}", _ftpserverip, _fileName)));uploadRequest.Credentials = new NetworkCredential(_ftpvideoid, _ftpvideopwd); //提供账号密码的验证uploadRequest.KeepAlive = false; //默认为true是上传完后不会关闭FTP连接uploadRequest.Method = WebRequestMethods.Ftp.DeleteFile;try{uploadRespose = (FtpWebResponse)uploadRequest.GetResponse();}catch (UriFormatException ex){return false;}catch (IOException ex){return false;}catch (WebException ex){return false;}finally{if (uploadRespose != null)uploadRespose.Close();if (_localfile != null)_localfile.Close();if (_uploadstream != null)_uploadstream.Close();}return true;}public static string UniqueName(string fileName){string GUID = Guid.NewGuid().ToString();string extensionName = Path.GetExtension(fileName);GUID += extensionName;return GUID;}
因为编译为类库,所以需要在web.config中的AppSettings节插入字段"ServerIP"表示IP地址,“FtpID”表示登录账户,“FtpPwd"表示登录密码。也可以直接按普通类进行调用,自己修改代码传入参数。需要优化的地方就是还未进行异步调用
版权声明:本文为博主原创文章,未经博主允许不得转载。 |
|
|