获取使用 ajax AsyncFileUpload 保存的文件的新文件名?

发布于 2024-10-08 04:50:13 字数 543 浏览 7 评论 0原文

我正在使用ajax工具包中的asyncfileupload ajax插件保存一个文件,当我保存它时,我正在更改文件名(以避免多个文件同名)。

文件上传后,用户需要知道文件的名称,因此我在 onclientuploadcomplete 事件中使用此 javascript 代码。

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

这是有效的,除非它获取的是旧名称,而不是新名称(由服务器端确定)。有什么办法让它返回新名称而不是旧名称?或者有什么办法可以实现这一目标?

这是我在获取新文件名后面的代码中的代码:

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

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒时光 2024-10-15 04:50:13

我从 http://forums.asp.net/post/4139037.aspx 它对我有用...

从那里复制代码:

<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" /> 

背后的代码:

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

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); 
}
凡尘雨 2024-10-15 04:50:13

将文件名写入代码隐藏中的隐藏字段并在客户端代码中读取该值怎么样?

How about writing the filename to a hiddenfield in the codebehind and the reading that value in your clientside code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文