Change file name of AJAX AsyncFileUpload control uploaded file in ASP.Net

Last Reply 4 months ago By dharmendr

Posted 4 months ago

hello,

i am using this async file upload control to bind image without page refresh.

 but it is not binding the image after upload please advice

<ajaxToolkit:AsyncFileUpload OnClientUploadComplete="uploadComplete" runat="server" ID="AsyncFileUpload1"
    Width="400px" UploaderStyle="Modern" CompleteBackColor="White" UploadingBackColor="#CCFFFF"
    ThrobberID="imgLoader" OnUploadedComplete="FileUploadComplete" OnClientUploadStarted = "uploadStarted" />


<asp:Image ID="imgLoader" runat="server" ImageUrl="~/images/loader.gif" /><br /><br />
<img id = "imgDisplay" alt="" src="" style = "display:none"/>
<asp:Label ID="Label1" runat="server" ></asp:Label>
    
<script type="text/javascript">
    function uploadStarted() {
        $get("imgDisplay").style.display = "none";
    }
    function uploadComplete(sender, args) {
        var imgDisplay = $get("imgDisplay");
        imgDisplay.src = "images/loader.gif";
        imgDisplay.style.cssText = "";
        var img = new Image();
        img.onload = function () {
            imgDisplay.style.cssText = "height:100px;width:100px";
            imgDisplay.src = img.src;
        };
        img.src = "<%=ResolveUrl(UploadFolderPath) %>" + args.get_fileName();
    }
</script>

 

    protected string UploadFolderPath = "~/Uploads/";
   
    protected void FileUploadComplete(object sender, EventArgs e)
    { 
        string fileName = Path.GetFileName(AsyncFileUpload1.FileName);
        string ext = Path.GetExtension(AsyncFileUpload1.FileName);
        if (ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png")
        {
            fileName = Guid.NewGuid().ToString() + ext;
        }
        AsyncFileUpload1.SaveAs(Server.MapPath("~/Uploads/" + fileName));
    }
Posted 4 months ago

Hi nauna,

The name with GUID is not possible to maintain the name to display the uploaded image.

Use TextBox control to manually insert the file name then its possible as you are doing. Refer below link.

Change (Rename) FileName of uploaded File when using AJAX AsyncFileUpload control in ASP.Net