Click here to Skip to main content
15,886,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In our website,we are using fileupload control to upload and download files.Before uploading it to the server,we are serialsing the content using XML serialiser and then encrypting the data using Rijndael algorithm and then save the file in server.I have set up the web.config httpRuntime with maxRequestLength="71680" executionTimeout="300"

The problem is that I can able to upload multiple files of size 10 mb.but when i try to upload file of size 40mb,it is throwing " out of memory " exception.

Memorystream is not holding up the filecontent.
Is there any way to do upload??

C#
//Serialise
      public string SerializeAnObject(object anObject, int fileLen)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlSerializer xml_Serializer = new XmlSerializer(anObject.GetType());
            MemoryStream memStream = new MemoryStream(fileLen);
            try
            {
                xml_Serializer.Serialize(memStream, anObject);
                memStream.Position = 0;
                xmlDoc.Load(memStream);
                return xmlDoc.InnerXml.ToString();
            }
            finally
            {
                memStream.Close();
            }
        }

// encryption
 public string Encrypt(string textToBeEncrypted, string strConn, int fileLen)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            string password = strConn;
            byte[] plainText = new byte[fileLen + 1];
            plainText = System.Text.Encoding.Unicode.GetBytes(textToBeEncrypted);
            byte[] salt = Encoding.ASCII.GetBytes(password.Length.ToString());
            PasswordDeriveBytes secretKey = new PasswordDeriveBytes(password, salt);        
// Creates a symmetric encryptor object.
            ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
           using (MemoryStream memoryStream = new MemoryStream(fileLen))
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(plainText, 0, plainText.Length);
 // Writes the final state and clears the buffer        
                    cryptoStream.FlushFinalBlock();
                    byte[] cipherBytes = memoryStream.ToArray();
                    memoryStream.Close();
                    cryptoStream.Close();
                    string encryptedData = Convert.ToBase64String(cipherBytes);
                    return encryptedData;
                }
            }
        }
Posted
Updated 4-Jun-10 6:26am
v2
Comments
[no name] 4-Jun-10 13:10pm    
Which part is throwing the exception, the serializing or the encrypting?
ramyasangeet13 5-Jun-10 7:51am    
That depends on the file size.If the file is of size 17Mb,then it is throwing error in encryption part.If the file size is 40 Mb,am getting error in serialising part.

1 solution

Aha! asp.net FileUpload. The asp.net file upload control does a decent job when you dealing small files to upload. As soon as you start increasing the file size, frastration level increases as well.

I dealt with this in the past and know very well your frustration. For starters read this one.[^]. Then I would consider replacing the file upload control another decent control. There are tons of FileUpload controls, both free and commericial.

In my case I ended up using asp.net file upload control by Darren Johnstone[^] and it works like a charm.
 
Share this answer
 
Comments
ramyasangeet13 5-Jun-10 8:06am    
Thanks for your reply Yousuf.I will have a look on Darren's file upload control.The problem in my website is serialisation and encryption part.MemoryStream is not holding up the content and throwing error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900