3
Reply

FileUpload Fails when trying to upload > 5MB files

gopal kannan

gopal kannan

Jun 24 2013 2:37 AM
2k
hi..

i m using silverligh appl. In that i need to upload files.
for uploading files i used handler file and passing the stream values to handler file in order to perform upload operation.

now when i try to upload greater than 5MB file then the upload fails.
and it throws the error "Invalid length for a Base-64 char array".

during uploading it come about 94% uploading and after that it throws the above error and the upload fails.

my handler file code is below:
public void ProcessRequest(HttpContext context)
{

try
{
context.Response.ContentType = "text/plain"; 
string check = context.Request["filestream"]; 

check = Regex.Replace(check, " ", "+"); 
byte[] bytes = Convert.FromBase64String(check);
string fileName = context.Server.MapPath("Uploads\\" + context.Request["filename"]);
context.Request["foldername"] + context.Request["filename"]);

FileStream fs;
if (!File.Exists(fileName))
fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
else
fs = new FileStream(fileName, FileMode.Append);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
fs.Dispose();
}
catch (Exception ex)
{
clsExHandler.Instance.Write(ex);

try
{
clsExHandler.Instance.Write("full Url: " + context.Request.Params.ToString() );
foreach (string ss in context.Request.ServerVariables.GetValues("QUERY_STRING"))
clsExHandler.Instance.Write("get url:" + ss.ToString());
}
catch (Exception)
{
}

}
}

-----------------

my silverlight code is below:
byte[] b = new byte[57344];
//byte[] b = new byte[1153433];

int bytesRead = fs.Read(b, 0, b.Length);
long bytesReadTotal = bytesRead;
while (bytesRead > 0)
{
// has request to stop early been made? 
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}

AutoResetEvent a = new AutoResetEvent(false);

WebClient wc = new WebClient();
wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
string errormessage = "";
wc.UploadStringCompleted += (s, e1) =>
{
string result = "";
if (e1.Error == null)
result = e1.Result;
else
{
errormessage = e1.Error.Message.ToString();
//MessageBox.Show(errormessage);
e.Cancel = true; // set worker.CancellationPending 
}
a.Set();
};

wc.UploadStringAsync(new Uri("/Uploader.ashx", UriKind.RelativeOrAbsolute), "POST"
, "foldername=" + "&filename=" + fileName + "&filestream="
+ Convert.ToBase64String(b, 0, bytesRead));

a.WaitOne();

int percentComplete = (int)((float)bytesReadTotal / (float)fs.Length * 100);
worker.ReportProgress(percentComplete, (Object)bytesReadTotal);
if (worker.CancellationPending)
{
e.Cancel = true;
break; // request is stopping 
}
if (bytesReadTotal > 1048576 && b.Length != 1153433)
{
b = new byte[1153433];
}
bytesRead = fs.Read(b, 0, b.Length);
bytesReadTotal += bytesRead;

--------------

how to handle this issue..

need ur suggestions..

Note:
i have already set the clientmaxreq.length to maximum value. but still we cannot upload >5MB file
regards
gopal.s

Answers (3)