none
ftp上传类 上传的文件都是0kb 请高手指点 代码如下 RRS feed

  • 问题

  • using System;
    using System.Configuration;
    using System.IO;
    using FtpSupport;
    using Microsoft.Win32;
    using System.Web;


        public class FtpClass
        {
            private string FtpIP=ConfigurationSettings.AppSettings["FtpIP"];
            private string FtpUserName=ConfigurationSettings.AppSettings["FtpUserName"];
            private string FtpPassord=ConfigurationSettings.AppSettings["FtpPassWord"];
            private FtpConnection ftp;

            private FtpConnection FtpConn()
            {
                ftp=new FtpConnection();
                ftp.Connect(this.FtpIP,this.FtpUserName,this.FtpPassord);
                return ftp;
            }

            private string GetFileExtName(string filename,bool withdot)
            {
                string [] arrs=filename.Split('.');
                int i=arrs.Length;
                return withdot?"."+arrs[i-1].ToString():arrs[i-1].ToString();
            }

            private string GetFileContentType(string filedownloadname)
            {
                string DEFAULT_CONTENT_TYPE = "application/unknown";
                RegistryKey regkey,fileextkey;
                string FileContentType;
                try
                {               
                    regkey=Registry.ClassesRoot;               
                    fileextkey=regkey.OpenSubKey(this.GetFileExtName(filedownloadname,false));               
                    FileContentType=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString();
                }
                catch
                {
                    FileContentType=DEFAULT_CONTENT_TYPE;
                }       
                return FileContentType;
            }

            public string FtpUpload(HttpPostedFile file,string dir,string fileName)
            {
                string FileDownloadName=fileName;
                FtpConnection ftp=this.FtpConn();
                if(ftp.DirectoryExist(dir))
                    ftp.SetCurrentDirectory(dir);
                else
                {
                    ftp.CreateDirectory(dir);
                    ftp.SetCurrentDirectory(dir);
                }
                ftp.PutStream(file.InputStream,FileDownloadName);
                ftp.Close();
                return FileDownloadName;
            }

            public void FileDel(string filedownloadname,string dir)
            {
                FtpConnection ftp=this.FtpConn();
                if(ftp.DirectoryExist(dir))
                {
                    ftp.SetCurrentDirectory(dir);
                    if(ftp.FileExist(filedownloadname))
                    {
                        ftp.DeleteFile(filedownloadname);
                    }
                }
                ftp.Close();
            }

            public void FtpDownload(HttpContext context,string filedownloadname,string dir)
            {
                context.Response.Clear();
                context.Response.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode(filedownloadname, System.Text.Encoding.UTF8));
                context.Response.ContentType=this.GetFileContentType(filedownloadname);
                FtpConnection ftp=this.FtpConn();
                ftp.SetCurrentDirectory(dir);
                if(ftp.FileExist(filedownloadname))
                {
                    FtpStream ftpfs=ftp.OpenFile(filedownloadname,GenericRights.Read);
                    byte [] buffer=new byte[10240];
                    int n=ftpfs.Read(buffer,0,buffer.Length);
                    while(n>0)
                    {
                        context.Response.BinaryWrite(buffer);
                        n=ftpfs.Read(buffer,0,buffer.Length);
                    }
                    context.Response.End();
                    ftpfs.Close();
                }
                else
                {
                    context.Response.Write("<script>alert('文件不存在!');</script>");
                }
                ftp.Close();
            }
        }

    2010年7月28日 2:28

答案

  •         public static void UploadFile(string FTPServerPath, string FTPUsername, string FTPPassword, string UploadFile)
            {
                FileStream fileStream = null;
                Stream FTPStream = null;
                try
                {
                    if (!File.Exists(UploadFile))
                    {
                        throw new Exception("");
                    }
                    FileInfo fileInfo = new FileInfo(UploadFile);
                    long FileSize = fileInfo.Length;
                    if (FileSize == 0)
                    {
                        throw new Exception("");
                    }
                    FTPServerPath = FTPServerPath + fileInfo.Name;
                    FtpWebRequest FtpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPServerPath));
                    FtpWebRequest.Credentials = new NetworkCredential(FTPUsername, FTPPassword);
                    FtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
                    FtpWebRequest.UseBinary = true;
                    FtpWebRequest.KeepAlive = false;
                    FtpWebRequest.ContentLength = FileSize;
                    int BufferSize = 4096;
                    byte[] UploadBuffer = new byte[BufferSize];
                    FTPStream = FtpWebRequest.GetRequestStream();
                    fileStream = fileInfo.OpenRead();
                    int contentLen = fileStream.Read(UploadBuffer, 0, BufferSize);
                    while (contentLen != 0)
                    {
                        FTPStream.Write(UploadBuffer, 0, contentLen);
                        contentLen = fileStream.Read(UploadBuffer, 0, BufferSize);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (FTPStream != null)
                    {
                        FTPStream.Close();
                    }
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                }
            }
    2010年7月28日 13:36