ImageUpload的问题(AjaxControlToolKit和AsyncFileUpload)

时间:2012-11-25 19:18:30

标签: c# asp.net ajaxcontroltoolkit image-uploading asyncfileupload

使用AjaxControlToolKit我的imageuplad有问题。 我可以上传文件并显示它们而不回发但如果用户改变了主意并上传了另一张图片,它仍会显示第一张图片。欢迎帮助。

这是我的代码: 前:

<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" %>

    <script type="text/javascript">
        function uploadComplete(sender, args) {
            var pathForUploadedImage = $('#uploadedHiddenField').val();                               
            $get("uploadImageImg").src = $('#uploadedHiddenField').val();
            $(".uploadImage").fadeIn(100);

        }
</script>

<asp:Image id="uploadImageImg" class="uploadImage" alt="" runat="server" ClientIDMode="Static" />
<div id="uploadedImageDiv"></div>
<asp:Image ID="loader" runat="server" ImageUrl="~/library/images/loading.gif" Style="display: None" />
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
<asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnClientUploadComplete="uploadComplete" OnUploadedComplete="AsyncFileUpload1_UploadedComplete" CompleteBackColor="white" />
<asp:HiddenField ID="uploadedHiddenField" ClientIDMode="Static"  runat="server"/>

返回:

 protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {

        try
        {

            Styling.SetupStyles();
            string virualFolder = "";
            if (string.IsNullOrEmpty(UploadDirectory))
            {
                virualFolder = "/" + Styling.GetStyleValue("customNewsImagesFolder", "") + "/";
            }
            else
            {
                virualFolder = "/" + Styling.GetStyleValue(UploadDirectory, "") + "/";
            }



            string fileExtension = Path.GetExtension(e.FileName);

            int fileSize = Convert.ToInt32(e.FileSize);

            string physicalFolder = Server.MapPath(virualFolder);

            string randomFileName = System.IO.Path.GetRandomFileName().Replace(".", "");

            bool isUnique = true;
            DirectoryInfo dir = new DirectoryInfo(MapPath(virualFolder));
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo myFile in files)
            {
                if (myFile.Name == randomFileName)
                {
                    isUnique = false;
                }
            }

            IsAllowedFileExtension(fileExtension);

            bool error = false;

            if (!isUnique)
            {
                error = true;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>alert('En fil med detta namn finns redan, var vänlig och döp om din fil');</script>", false);
            }
            else if (!IsAllowedMaxFileSize(fileSize))
            {
                error = true;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>alert('Filen är för stor, vänligen försök igen med en fil som är mindre än 2 MB');</script>", false);
            }
            else if (!IsAllowedFileExtension(fileExtension))
            {
                error = true;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>alert('Felaktigt filformat, vänligen försök igen med en fil av följande typ: .jpg .jpeg .png .gif');</script>", false);
            }

            if (!error)
            {
                AsyncFileUpload1.SaveAs(physicalFolder + randomFileName + fileExtension);
                string thePath = (virualFolder + randomFileName + fileExtension).Replace("\\", "/");

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "filePath", "top.$get(\"" + uploadedHiddenField.ClientID + "\").value = '" + thePath + "';", true);                

            }
        }
        catch (Exception ex)
        {
            erLog.LogException(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
        }
    }

    private bool IsAllowedFileExtension(string fileExtension)
    {
        bool valid = false;
        if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".jpeg" || fileExtension.ToLower() == ".gif" || fileExtension.ToLower() == ".png")
            valid = true;
        return valid;
    }

    private bool IsAllowedMaxFileSize(int fileSize)
    {
        bool valid = false;
        if (fileSize < 2100000)
            valid = true;
        return valid;
    }

我认为问题与我使用“ScriptManager.RegisterClientScriptBlock”显示图像有关 - 但是如果脚本已经在DOM中,它似乎没有注册脚本。

1 个答案:

答案 0 :(得分:2)

您没有指定AsyncFileUpload1_UploadedComplete事件处理程序是在控件内还是在页面内。如果它在内部控制而不是修复您的问题,您可以尝试使用页面实例注册客户端脚本。其他选项是每个部分回发生成唯一的脚本ID。

选项1(使用页面实例注册客户端脚本):

ScriptManager.RegisterClientScriptBlock(this, this.Page, "filePath", "top.$get(\"" + uploadedHiddenField.ClientID + "\").value = '" + thePath + "';", true);

选项2(每个部分回发生成唯一的脚本ID):

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "top.$get(\"" + uploadedHiddenField.ClientID + "\").value = '" + thePath + "';", true);