반응형

HttpPostedFile 타입으로 매개변수를 받아 그대로 외부 서버로 FTP 전송을 하기 위해서 아래와 같은 코드로

작성을 해야한다.

서버 내부의 파일을 가지고 FTP 전송하는 방법이랑 조금 다르다ㅜㅜ

 

private bool UploadToFTP(HttpPostedFile fileToUpload)
       {

           try
          
{
               string uploadUrl = @ftp://블라블라블;
               string uploadFileName = fileToUpload.FileName;

               Stream streamObj = fileToUpload.InputStream;
               Byte[] buffer = new Byte[fileToUpload.ContentLength];
               streamObj.Read(buffer, 0, buffer.Length);
               streamObj.Close();
               streamObj = null;

               string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
               FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
               requestObj.Method = WebRequestMethods.Ftp.UploadFile;
               requestObj.Credentials = new NetworkCredential("userid", "password");
               Stream requestStream = requestObj.GetRequestStream();
               requestStream.Write(buffer, 0, buffer.Length);
               requestStream.Flush();
               requestStream.Close();
               requestObj = null;

               return true;
           }
           catch
          
{
               return false;
           }
       }

반응형

+ Recent posts