This is a migrated thread and some comments may be shown as answers.

My postback issues

1 Answer 93 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 21 Jun 2011, 08:54 PM
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:

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,

1 Answer, 1 is accepted

Sort by
0
Peter Filipov
Telerik team
answered on 24 Jun 2011, 12:28 PM
Hi Chris,

It is really strange behavior.
You can use the following code to save the uploaded file:
e.File.SaveAs("file path - e.g. C:\\image.jpg");
OnFileUploaded event is fired for every uploaded file(if there are some).
If you are using RadAjaxManager on your page please make sure the you update RadAsyncUpload after an ajax postback.
In case you continue to experience that issue, please open a support ticket and send us a working sample project for a local investigation.

Best wishes,
Peter Filipov
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
AsyncUpload
Asked by
Chris
Top achievements
Rank 1
Answers by
Peter Filipov
Telerik team
Share this question
or