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

Exception on IE Refresh

3 Answers 31 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Marc
Top achievements
Rank 1
Marc asked on 27 May 2013, 02:25 PM
Hi, I am using RadAsyncUpload. After the user select a file I automatically make a postback using Ajax using where btnFileUploaded is not visible.

function asupImage_ClientFileUploaded(sender, args) {
    $find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>").ajaxRequestWithTarget('<%= btnFileUploaded.UniqueID %>', '');
}

On server side OnFileUploaded is called and I save the file this way

protected void asupImage_FileUploaded(object sender, FileUploadedEventArgs e)
{
    string strAppplicationPath = HttpContext.Current.Request.ApplicationPath;
    string strLocalPath = HttpContext.Current.Request.MapPath(strAppplicationPath);
    e.File.SaveAs(strLocalPath + @"\Temp\" + e.File.FileName, true);
}

...which works. But if after that I hit the refresh button in IE (F5) whitout having select a new file then OnFileUploaded is called again and an Exception occurred in SaveAs() because the file doesn't exists anymore.

Do I miss something or is it a bug?

3 Answers, 1 is accepted

Sort by
0
Hristo Valyavicharski
Telerik team
answered on 28 May 2013, 03:26 PM
Hi Marc,

This behavior is expected. When you call e.File.SaveAs() in the asupImage_FileUploaded event uploaded file is moved from the Temporary to the Target folder. Then if you refresh your browser, it posts the form again and FileUploaded event is fired. This time the temp file does not exist as it was removed first time when event was fired.

There are a few thing you could do to resolve the issue:
  • Save the input stream instead of using e.File.Save(). This would keep uploaded file in the temp folder. For more details please read this article.
  • Detect page refresh and execute your code only if the page is not refreshed.

Regards,
Hristo Valyavicharski
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Marc
Top achievements
Rank 1
answered on 28 May 2013, 04:44 PM
Hi, thanks for the response. It works.

Is there a way to get the file on the server side via a web method instead of using a postback? Postback cause some troubles in my page and also it's faster with a web method.
0
Hristo Valyavicharski
Telerik team
answered on 30 May 2013, 11:56 AM
Hi Marc,

To avoid the post back and transfer file to the server, you could use a Custom Http Handler. There is an online demo showing this functionality. And this article explains how to implement it.

Regards,
Hristo Valyavicharski
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
AsyncUpload
Asked by
Marc
Top achievements
Rank 1
Answers by
Hristo Valyavicharski
Telerik team
Marc
Top achievements
Rank 1
Share this question
or