FCKeditor 上傳ftp asp.net

taogchan發表於2011-11-21

修改FileBrowser/fileworkerbase.cs的FileUpload方法:

protected void FileUpload( string resourceType, string currentFolder, bool isQuickUpload )
  {
   HttpPostedFile File = Request.Files[ "NewFile" ];
            System.Drawing.Image img = System.Drawing.Image.FromStream(oFile.InputStream);
   string sFileName = "";

   if ( File == null )
   {
    this.SendFileUploadResponse( 202, isQuickUpload );
    return;
   }

   // Map the virtual path to the local server path.
   string sServerDir = this.ServerMapFolder( resourceType, currentFolder, isQuickUpload );

   // Get the uploaded file name.
   sFileName = System.IO.Path.GetFileName( oFile.FileName );
   sFileName = this.SanitizeFileName( sFileName );

   string sExtension = System.IO.Path.GetExtension( oFile.FileName );
   sExtension = sExtension.TrimStart( '.' );

   if ( !this.Config.TypeConfig[ resourceType ].CheckIsAllowedExtension( sExtension ) )
   {
    this.SendFileUploadResponse( 202, isQuickUpload );
    return;
   }

   if ( this.Config.CheckIsNonHtmlExtension( sExtension ) && !this.CheckNonHtmlFile( oFile ) )
   {
    this.SendFileUploadResponse( 202, isQuickUpload );
    return;
   }

   int iErrorNumber = 0;
   int iCounter = 0;
            string date = DateTime.Now.ToString("yyyyMMddhhmmss");
            string sFileNameAll = System.IO.Path.GetFileName(oFile.FileName);
            string imageFullName = date + "_" + sFileNameAll;
            string rginalNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(imageFullName);

   while ( true )
   {
                string sFilePath = System.IO.Path.Combine(sServerDir, imageFullName);

    if ( System.IO.File.Exists( sFilePath ) )
    {
     iCounter++;
                    imageFullName =
                        orginalNameWithoutExt +
      "(" + iCounter + ")." +
      sExtension;

     iErrorNumber = 201;
    }
    else
    {

                    bool con = ftpuploadAll(img, image_xq + imageFullName);//上傳到ftp
                    if (con == false)
                    {
                        iErrorNumber = 400;
                    }
                    else
                    {

                        oFile.SaveAs(sFilePath);//上傳到本地
                        iErrorNumber = 0;
                    }
                  
                    break;
    }
   }

   TypeConfig typeConfig = this.Config.TypeConfig[resourceType] ;

   string sFileUrl = isQuickUpload ? typeConfig.GetQuickUploadPath() : typeConfig.GetFilesPath() ;
            sFileUrl += imageFullName;

            this.SendFileUploadResponse(iErrorNumber, isQuickUpload, sFileUrl, imageFullName);
  }

 

FTP方法:

在webconfig配置ftp的資訊,在這裡呼叫

public static readonly string ftpuser = ConfigurationManager.AppSettings["ftp_user"].ToString();//ftp user
        public static readonly string ftppassword = ConfigurationManager.AppSettings["ftp_password"].ToString();//ftp password
        public static readonly string image_xq = ConfigurationManager.AppSettings["image_xq"].ToString();//ftp address

public bool ftpuploadAll(System.Drawing.Image image, string uri)
        {
            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            reqFTP.Credentials = new NetworkCredential(ftpuser, ftppassword);
            reqFTP.KeepAlive = false;
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            //例項化流
            MemoryStream imageStream = new MemoryStream();
            //將圖片的例項儲存到流中           
            image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //儲存流的二進位制陣列
            byte[] imageContent = new Byte[imageStream.Length];

            imageStream.Position = 0;
            //將流瀉如陣列中
            imageStream.Read(imageContent, 0, (int)imageStream.Length);
            byte[] buff = imageStream.ToArray();
            try
            {
                Stream strm = reqFTP.GetRequestStream();
                strm.Write(buff, 0, buff.Length);
                strm.Close();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22392018/viewspace-711710/,如需轉載,請註明出處,否則將追究法律責任。

相關文章