0

我正在尝试使用 AjaxControlToolkit 中的 AsyncFileUpload 组件将文件上传到服务器后设置标签文本值。但是它接缝它是无效的,虽然上传后文件上传器是绿色的,并且上传工作。

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    string filename  = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);
    AsyncFileUpload1.SaveAs(Server.MapPath("Uploads/") + filename); 
    sourceLabel.Text="saved to "+filename; //this has no effect. I assume this is because the event is Async, but how can I set the value of  sourceLabel?
}
4

2 回答 2

0

AsyncFileUpload 控件使用隐藏框架进行文件提交,因此所有控件更新都将丢失。检查此链接并注意uploadResult标签的文本如何从 AsyncFileUpload1_UploadedComplete方法更改:Ajax Control Toolkit source code

于 2013-02-08T08:49:07.657 回答
0

它对我有用:https ://stackoverflow.com/a/12472235/2247978

..................................................... ..................................................... .

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

..................................................... ..................................................... .

protected void BtnUpload_Click(object sender, EventArgs e)
{
    UploadMessage.Text = UploadedPathHiddenField.Value;
}
于 2015-04-19T04:19:38.270 回答