如何获得服务器端的重命名文件名,在客户方在使用AsyncFileUpload [英] How to get serverside renaming filename in clientside while using AsyncFileUpload

查看:175
本文介绍了如何获得服务器端的重命名文件名,在客户方在使用AsyncFileUpload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器端code

protected void UploadComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    rlativepath =GeneratePrefixFileName() + AsyncFileUpload1.PostedFile.FileName;
    databasepath = "~/Image/" + rlativepath;
    filePath = Request.PhysicalApplicationPath + "image\\"+rlativepath;
    AsyncFileUpload1.SaveAs(filePath);
}

客户端code

client side code

<script type="text/javascript">

function upLoadStarted() {
    $get("imgDisplay").style.display = "none";
}
function showConfirmation(sender, args) {
    var txt = document.getElementById('<%=statusoutput.ClientID %>');
    var img = document.getElementById('<%=statusoutput.ClientID %>');
    txt.value = "Upload Successful";

    var imgDisplay = $get("imgDisplay");
    imgDisplay.src = "ajaxupload.jpg";
    imgDisplay.style.cssText = "height:100px;width:100px";
    var img = new Image();
    img.onload = function () {
        imgDisplay.style.cssText = "height:100px;width:100px";
        imgDisplay.src = img.src;
    };
    <%# UploadFolderPath1+=rlativepath %>
    img.src = "<%=ResolveUrl(UploadFolderPath1) %>"+ args.get_fileName();
    alert(img.src);
    var imagedescrip = $get("imagedescrip");
    imagedescrip.style.cssText = "visibility:visible;";
    }


aspx页面:

aspx page:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
        content of page
    FNever increase, beyond what is necessary, the number of entities required to explain anything." William of Ockham (1285-1349)
    <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnUploadedComplete="UploadComplete" ThrobberID="imgLoader" OnClientUploadStarted="upLoadStarted" UploaderStyle="Modern" OnClientUploadComplete="showConfirmation"
     Width="400px" CompleteBackColor="White" UploadingBackColor="#CCFFFF"></asp:AsyncFileUpload>
    <input type="text" id="statusoutput" runat="server" readonly="readonly" tabindex="-1000" style="border:0px;background-color:transparent;" /> 
    <asp:Image ID="imgLoader" runat="server" ImageUrl="ajaxupload.jpg" Height="100px" Width="100px" />

    <img id = "imgDisplay" alt="" src="" style = "display:none;height:100px;width:100px"/>

我使用AsyncFileUpload上传文件,保存在服务器上的文件之前,我重命名​​选定的文件。我怎样才能在客户端这个新的文件名?我现在的问题是我没有得到与args.get_filename()在客户端side.how的重命名文件名得到它?

I'm using AsyncFileUpload for uploading files, before saving file on server, i rename the selected file. How can I get this new file name in the client side? now my problem is i am not getting the renaming filename with args.get_filename() in client side.how to get it?

推荐答案

添加HiddenField控件到窗体:

Add HiddenField control onto a form:

<asp:HiddenField runat="server" ID="UploadedPathHiddenField" />

重写 UploadComplete 方法如下:

protected void UploadComplete(object sender, AsyncFileUploadEventArgs e)
{
    var fileName = GeneratePrefixFileName() +  System.IO.Path.GetFileName(e.FileName);
    var relativePath = "~/Image/" + fileName;
    var filePath = Server.MapPath(relativePath);
    AsyncFileUpload1.SaveAs(filePath);
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "filePath", "top.$get(\"" + UploadedPathHiddenField.ClientID + "\").value = '" + ResolveClientUrl(relativePath) + "';", true);
}

之后,你可以通过在showConfirmation方法保存的图像的路径:

After that you can get path of saved image in showConfirmation method by :

var src = $get("<%= UploadedPathHiddenField.ClientID %>").value;

这篇关于如何获得服务器端的重命名文件名,在客户方在使用AsyncFileUpload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆