gpt4 book ai didi

c# - 如何在使用 AsyncFileUpload 时在客户端获取服务器端重命名文件名

转载 作者:太空宇宙 更新时间:2023-11-03 21:55:33 31 4
gpt4 key购买 nike

服务器端代码

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

客户端代码

<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 页面:

<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() 获取重命名文件名。如何获取它?

最佳答案

将 HiddenField 控件添加到窗体上:

<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 方法获取保存图片的路径:

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

关于c# - 如何在使用 AsyncFileUpload 时在客户端获取服务器端重命名文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12471506/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com