FileUpload上传两次

时间:2014-03-16 11:50:14

标签: c# asp.net

这就是我想要做的。使用Ajax AsyncFilepload上载图像时,它还会在图像控件中显示图像。由于上传是异步的,虽然图片上传很好,但它并没有显示在图像Control中。因此,根据建议,添加了JS函数。它现在很好,但只是C#代码触发两次,因为回发,因此每个图像上传两次。

此外,为什么ViewState第二次变为null。此页面使用主页。

JS:

function UpdateImage()
{
  __doPostBack("UpdatePanel2","");
}

ASPX:

<asp:AsyncFileUpload ID="fu" runat="server" 
 OnClientUploadComplete="UpdateImage" OnUploadedComplete="fu_UploadedComplete"/>

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
 <ContentTemplate>
  <asp:Image runat="server" ID="img"/>
 </ContentTemplate>
</asp:UpdatePanel>

C#:

protected void fu_UploadedComplete(object sender, 
                                 AjaxControlToolKit.AsyncFileUploadEventArgs e)
{
   if(fu.HasFile && ViewState["vMap"] == null)
    {
      string sNew = getRandomNumber().ToString() + fu.FileName;
      ViewState["vMap"] = sNew;
      sNew = "~/Temp/"+sNew;
      fu.SaveAs(Server.MapPath(sNew));
      img.ImageUrl = sNew;
      UpdatePanel.Update();
    }
}

1 个答案:

答案 0 :(得分:0)

HiE frnd ... !!!!!!!
        您的文件上传控件的ID是“fu”,而您使用的是“fileUpload.HasFile”,这是错误的。
        因此输出不正确。我有一些代码尝试这个。
        * HEAD TAG *

    <script type="text/javascript">
            // This function will execute after file uploaded successfully
            function uploadComplete()
             {
                document.getElementById('<%=lblMsg.ClientID %>').innerHTML = "File Uploaded Successfully";
            }
            // This function will execute if file upload fails
            function uploadError() {
                document.getElementById('<%=lblMsg.ClientID %>').innerHTML = "File upload Failed.";
            }
        </script>

BODY

<body>
    <form id="form1" runat="server">
    <ajax:ToolkitScriptManager ID="scriptManager1" runat="server" />
    <div>
        <ajax:AsyncFileUpload ID="fileUpload1" OnClientUploadComplete="uploadComplete" OnClientUploadError="uploadError"
            CompleteBackColor="White" Width="350px" runat="server" UploaderStyle="Modern"
            UploadingBackColor="#CCFFFF" ThrobberID="imgLoad" OnUploadedComplete="fileUploadComplete" />
        <br />
        <asp:Image ID="imgLoad" runat="server" ImageUrl="loading.gif" />
        <br />
        <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>

文件背后的代码

protected void fileUploadComplete(object sender, AsyncFileUploadEventArgs e)
    {
        string filename = System.IO.Path.GetFileName(fileUpload1.FileName);
        fileUpload1.SaveAs(Server.MapPath("Files/") + filename);
    }