I'm using the RadAsyncUpload on a webform that have several components to perform certain operations. Initially, when I load the file, the FileUploaded is fired on the first postback as expected, and the file is copied to the intended directory in the server. This is my code for this part:
The problem is that there are other components that fire the postback event on the same page after the file upload, which fires over and over again the FileUploaded event, with the big problem that after the second time it postbacks it generates an exception at this line:
Which has sense since the file was copied already on the first postback, so the InputStream is not usable anymore. Can you recommend me a way to make sure that this line is excuted only when a file was actually uploaded and not on every single postback event? hopefully something more elegant of what I did for now: just putting a try-catch block and ignoring the error.
Thanks,
protected void AsyncUpload1_FileUploaded(object sender, Telerik.Web.UI.FileUploadedEventArgs e)
{
using (Stream stream = e.File.InputStream)
{
bool fileSaved = false;
Random objRand = new Random();
while (!fileSaved)
{
string strUploadFileName = "~/tmp/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
if (!File.Exists(Server.MapPath(strUploadFileName)))
{
byte[] xlsData = new byte[stream.Length];
stream.Read(xlsData, 0, (int)stream.Length);
FileStream fs = new FileStream(Server.MapPath(strUploadFileName), FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(xlsData);
bw.Close();
ViewState["currXLFile"] = Server.MapPath(strUploadFileName);
//read contents now...
LoadData();
fileSaved = true;
}
}
}
}
The problem is that there are other components that fire the postback event on the same page after the file upload, which fires over and over again the FileUploaded event, with the big problem that after the second time it postbacks it generates an exception at this line:
using (Stream stream = e.File.InputStream)
Which has sense since the file was copied already on the first postback, so the InputStream is not usable anymore. Can you recommend me a way to make sure that this line is excuted only when a file was actually uploaded and not on every single postback event? hopefully something more elegant of what I did for now: just putting a try-catch block and ignoring the error.
Thanks,