I am trying to send batch of files along with various other form elements over asynchronous XMLHTTPRequest. Each batch can contain file size up to 15 MB. There can be multiple batches.
I am reading the files in a javascript and converting them into base64 string and then trying to receive the files in the controller (using for loop) and converting them into byte array.
string fileValue1 = form.GetValues("fileName1");
string fileValue2 = form.GetValues("fileName2");
The file value is of the format as below, depending upon the type of the attachment:
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkC...
Using the below substring to remove the content before ',' and doing some processing before converting it into byte array.
fileValue1 = fileValue1 .Substring(fileValue1 .IndexOf(',') + 1);
fileValue1 = fileValue1 .Trim().Replace(" ", "+");
fileValue1 = Convert.FromBase64String(fileValue1)
Since I am sending files through multiple batches (say if there are 10 batches), there will be total of around 150 to 200 MB of files trying to reach the controller through asynchronous AJAX calls.
While sending, I am getting the below error message:
System.OutOfMemoryException was thrown at converting base64 string to byte array - occuring at replace statement.
I have followed various posts for workaround, but nothing seems to be working for me. I tried to increase "httpRuntime maxRequestLength" and " maxAllowedContentLength" in web.config to 4 GB to allow huge size, but nothing seems to be working.
If I remove the line
fileValue1 = fileValue1 .Substring(fileValue1 .IndexOf(',') + 1);
I am getting below error:
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
Maximum size of file can reach upto 500 MB when sent through multiple batches.
I am not sure, how to read this string file by block to make sure memory is available before converting to byte array. Any help would be greatly appreciated.