简体   繁体   中英

Getting the new filename of a file saved with ajax AsyncFileUpload?

I'm saving a file with the asyncfileupload ajax plugin from the ajax toolkit and when I save it I'm changing the filename (to avoid multiple files with the same name).

After the file is uploaded, the user needs to know what the file has been named so I'm using this javascript code on the onclientuploadcomplete event.

function UploadComplete(sender, args) {
    alert(args.get_fileName());
}

This works except it gets the old name, not the new name (which is determined server-side). Is there any way to get it to return the new name rather than the old name? Or any work around to achieve this?

This is my code in the code behind the get the new filename:

string filename = DateTime.Now.ToString("dMyHmsf") + e.filename;
string strPath = MapPath("~/SavedImages/") + filename;    
AsyncFileUpload1.SaveAs(strPath);

I got the answer from http://forums.asp.net/post/4139037.aspx It works for me...

copied code from there:

<asp:ToolkitScriptManager runat="server">  
    </asp:ToolkitScriptManager>  


<!--This script snippet must be located below the ScriptManager-->  
    <script type="text/javascript">  
        Sys.Extended.UI.AsyncFileUpload.prototype.newFileName = null;  //I did not use this line
        function uploadcomplete(sender, e) {  
            alert(sender.newFileName);  
        }  
    </script>  


    <asp:AsyncFileUpload ID="AsyncFileUpload1" OnClientUploadComplete="uploadcomplete" 

        runat="server" OnUploadedComplete="AsyncFileUpload1_UploadedComplete1" /> 

the code behind:

protected void AsyncFileUpload1_UploadedComplete1(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e) 
{ 
    ScriptManager.RegisterClientScriptBlock(this, 
        this.GetType(), "newfile",  
        "window.parent.$find('" + AsyncFileUpload1.ClientID + "').newFileName='newfile.jpg';", true); 
}

如何将文件名写入代码隐藏中的隐藏字段以及读取客户端代码中的值?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM