farshad
farshad

Reputation: 1399

How to upload large files to FTP server in ASP MVC

I am developing an ASP MVC website. Now i need to upload files(.zip files) to my FTP server. For uploading i use this code.

This code uploads only those files which has size < 10 Mb.

for Example: when i upload a file with 150 MB size with this code it get damaged and the file size changed to 300 MB on my Ftp Server.

So can any one help me..

 byte[] fileBytes = null;

                //Read the FileName and convert it to Byte array.
                string filename = Path.GetFileName(FileUpload1.FileName);

                using (StreamReader fileStream = new StreamReader(FileUpload1.InputStream))
                {
                    fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
                    fileStream.Close();
                }

                try
                {
                    //Create FTP Request.
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + "/" + fileName);
                    request.Method = WebRequestMethods.Ftp.UploadFile;

                    //Enter FTP Server credentials.
                    request.Credentials = new NetworkCredential(ftpUName, ftpPWord);
                    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();
                    }

                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                    response.Close();
                }
                catch (WebException ex)
                {
                    throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
                }

Upvotes: 1

Views: 2004

Answers (2)

user1519979
user1519979

Reputation: 1874

It probably gets corrupt, because you are reading the data with utf8 encoded. You should read it in binary format.

Don't use:

using (StreamReader fileStream = new StreamReader(FileUpload1.InputStream))
{
   fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
   fileStream.Close();
}

You have to use File.ReadAllBytes or a BinaryReader(Stream)

https://msdn.microsoft.com/en-us/library/system.io.file.readallbytes(v=vs.110).aspx

https://msdn.microsoft.com/de-de/library/system.io.binaryreader(v=vs.110).aspx

for your example:

   byte[] fileBytes = File.ReadAllBytes(Path.GetFileName(FileUpload1.FileName));
   try
   {
          //Create FTP Request.
          FtpWebRequest request = (FtpWebRequest)...

Upvotes: 1

Karthik Ganesan
Karthik Ganesan

Reputation: 4222

Add this to your web.config

<httpRuntime maxRequestLength="whatever value you need in kb max value is 2,147,483,647 kb" relaxedUrlToFileSystemMapping="true" />

under system.web section the default is 4 mb size limit

Details here

Upvotes: 4

Related Questions