Exception on IE Refresh

4 posts, 0 answers
  1. Marc
    Marc avatar
    39 posts
    Member since:
    Nov 2012

    Posted 27 May 2013 Link to this post

    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?

  2. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 28 May 2013 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Marc
    Marc avatar
    39 posts
    Member since:
    Nov 2012

    Posted 28 May 2013 Link to this post

    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.
  5. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 30 May 2013 Link to this post

    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.
Back to Top