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

Uploaded file not refreshing- solution

1 Answer 59 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 1
Bill asked on 07 Jan 2012, 11:08 PM
Telerik:
If you are monitoring this forum and theres a better way, please let me know...

When uploading an image and overwriting the old file with the same filename, and you find that the file displayed on the upload page is not refreshing, you may find this solution useful.

You may get that for 2 reasons:
1. The upload filename is the same as the previous so the browser does not refresh the image
2. The file extension changes but the page still displays the old image

The code below does 2 additional things (compared to telerik sample code)
1. It re-sets the image path after uploading (with file ext that may have changed)
2. It adds a unique query string to the end of the image source to force browser refresh

      

       function BannerUploaded(sender, args) {
            var ajaxmgr = $find("<%= RadAjaxManager1.ClientID %>");
            var upload = $find("<%= RadAsyncUploadBannerImage.ClientID %>");
            var Extension;
            ajaxmgr.ajaxRequest("client1");
            $telerik.$(".invalid").html("");
            //Get the uploaded file ext in case the ext is different (different filename)
             var inputs = upload.getUploadedFiles();
            for (i = inputs.length - 1; i >= 0;i--) {Extension=getFileExtension(inputs[i]);}
         
            setTimeout(function () {
            sender.deleteFileInputAt(0);
            var mySrc = document.getElementById('<%= thumbnailBanner.ClientID %>').src;
            var FileWOExt=removeFileExtension(mySrc);
            document.getElementById('<%= thumbnailBanner.ClientID %>').src=FileWOExt + '.' + Extension + "?" + new Date().getTime();}, 400);
        }
 
        function validationFailedBanner(sender, args) {
 
            $telerik.$(".invalid")
            .html("Please choose a valid image file");
            sender.deleteFileInputAt(0);
        }
 
        function getFileExtension(filename) {
            return filename.split('.').pop();
        }
         
        function removeFileExtension(filename) {
         
        return filename.substr(0, filename.lastIndexOf('.'));
}

 

 

 

1 Answer, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 11 Jan 2012, 05:01 PM
Hello Bill,

Thank you for sharing this solution with the community. As a token of gratitude your Telerik points are updated.

Kind regards,
Dimitar Terziev
the Telerik team
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
Bill
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
Share this question
or