Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

c# - How to get serverside renaming filename in clientside while using AsyncFileUpload

server sidecode

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add HiddenField control onto a form:

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

Rewrite UploadComplete method as below:

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...