1
public void UploadFTPTextFile(string ftpServer, string ftpFolder, string user, string passward, string NName, FileUpload FileUpload1)
        {
            byte[] fileBytes = null;
            string fileName = NName;
            using (StreamReader fileStream = new StreamReader(FileUpload1.PostedFile.InputStream))
            {
                fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
                fileStream.Close();
            }   
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer + ftpFolder + fileName);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(user, passward);
                request.ContentLength = fileBytes.Length;
                request.UsePassive = true;
                request.UseBinary = true;
                request.ServicePoint.ConnectionLimit = fileBytes.Length;
                request.EnableSsl = false;
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(fileBytes, 0, fileBytes.Length);
                    requestStream.Close();
                }
        }

I am try upload the pdf file using above code , it upload successfully , but when download it from Web server and open it in acrobat reader , it show error , file is corrupt ? please help me to upload pdf file using FTP mode in asp.net.

1 Answer 1

1
public void UploadFTPPdfFile(string ftpServer, string ftpFolder, string user, string passward, string NewName, FileUpload FileUpload1)
        {
            string fileName = NewName;
            System.Net.FtpWebRequest rq = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpServer + ftpFolder + fileName);
            rq.Credentials = new System.Net.NetworkCredential(user,passward);
            rq.Method = System.Net.WebRequestMethods.Ftp.UploadFile;           
            System.IO.Stream fs = FileUpload1.PostedFile.InputStream;
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
            System.IO.Stream ftpstream = rq.GetRequestStream();
            ftpstream.Write(buffer, 0, buffer.Length);
            ftpstream.Close();
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.