Asyncfileupload 文件预览不显示

发布于 2024-11-06 13:13:56 字数 1772 浏览 0 评论 0原文

我在更新面板中有一个 Ajax 异步文件上传控件。我的上传工作正常,但上传完成后,我需要查看我上传的图像。 但它在这里不起作用,这是我所做的

function UploadComplete(sender, args) {
        var filename = args.get_fileName();
        var contentType = args.get_contentType();
        if (contentType.indexOf('image') == -1) {
            document.getElementById('<%=lblStatus.ClientID%>').innerText = "Uploaded file must be an Image!"+ "<span style='color:red;'>" + args.get_errorMessage() + "</span>";
            document.getElementById('<%=AsyncFileUpload1.ClientID%>').text.style.backgroundColor = "Red";
        }
        else {
            var text = "" + filename + "      |      " + args.get_length() + " bytes"+"Uploaded Succesfully";
            document.getElementById('<%=lblStatus.ClientID%>').innerText = text;
              $get("imageView1").src = "./~/" + filename;
        }
    }

AspCode:

<ajaxToolkit:AsyncFileUpload ID="AsyncFileUpload1" Width="400px" runat="server"    
        OnClientUploadError="uploadError"    
        OnClientUploadStarted="StartUpload"  
        OnClientUploadComplete="UploadComplete"  
        CompleteBackColor="Lime" UploaderStyle="Modern"    
        ErrorBackColor="Red" ClientIDMode="AutoID"    
        ThrobberID="Throbber"    
        UploadingBackColor="#66CCFF" 
            onuploadedcomplete="AsyncFileUpload1_UploadedComplete" /> 

 <asp:Label ID="Throbber" runat="server" Style="display: none">
 <asp:Image runat="server" ID="imgPreview" ImageUrl="~/Images/uploading.gif" />
        </asp:Label>
        <img runat="server" id="imageView1"/>
 <asp:Label ID="lblStatus" runat="server" Style="font-family: Arial; font-size: small;"></asp:Label>

I am having an Ajax Asynchronous file upload control in update panel. My upload works fine but after the upload is completed., I need to see the image I have uploaded.
But it doesnt work here is what I have done

function UploadComplete(sender, args) {
        var filename = args.get_fileName();
        var contentType = args.get_contentType();
        if (contentType.indexOf('image') == -1) {
            document.getElementById('<%=lblStatus.ClientID%>').innerText = "Uploaded file must be an Image!"+ "<span style='color:red;'>" + args.get_errorMessage() + "</span>";
            document.getElementById('<%=AsyncFileUpload1.ClientID%>').text.style.backgroundColor = "Red";
        }
        else {
            var text = "" + filename + "      |      " + args.get_length() + " bytes"+"Uploaded Succesfully";
            document.getElementById('<%=lblStatus.ClientID%>').innerText = text;
              $get("imageView1").src = "./~/" + filename;
        }
    }

AspCode:

<ajaxToolkit:AsyncFileUpload ID="AsyncFileUpload1" Width="400px" runat="server"    
        OnClientUploadError="uploadError"    
        OnClientUploadStarted="StartUpload"  
        OnClientUploadComplete="UploadComplete"  
        CompleteBackColor="Lime" UploaderStyle="Modern"    
        ErrorBackColor="Red" ClientIDMode="AutoID"    
        ThrobberID="Throbber"    
        UploadingBackColor="#66CCFF" 
            onuploadedcomplete="AsyncFileUpload1_UploadedComplete" /> 

 <asp:Label ID="Throbber" runat="server" Style="display: none">
 <asp:Image runat="server" ID="imgPreview" ImageUrl="~/Images/uploading.gif" />
        </asp:Label>
        <img runat="server" id="imageView1"/>
 <asp:Label ID="lblStatus" runat="server" Style="font-family: Arial; font-size: small;"></asp:Label>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓味的萝莉 2024-11-13 13:13:56

您可以使用 OnUploadedComplete 事件来显示图像。

<ajaxToolkit:AsyncFileUpload ID="AsyncFileUpload1" Width="400px" runat="server" OnUploadedComplete="ProcessUpload"

protected void ProcessUpload(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
string fileName = Server.MapPath("./") + "image.jpg";
AsyncFileUpload1.SaveAs(fileName);

ScriptManager.RegisterClientScriptBlock(AsyncFileUpload1, AsyncFileUpload1.GetType(), "img",
    "top.document.getElementById('imgUpload').src='image.jpg';", 
    true);
}

有关如何显示预览的详细信息,请查看以下示例:使用 AsyncFileUpload 控件在 ASP.NET 中上传 AJAX 文件

You can use the OnUploadedComplete event to display the image.

<ajaxToolkit:AsyncFileUpload ID="AsyncFileUpload1" Width="400px" runat="server" OnUploadedComplete="ProcessUpload"

protected void ProcessUpload(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
string fileName = Server.MapPath("./") + "image.jpg";
AsyncFileUpload1.SaveAs(fileName);

ScriptManager.RegisterClientScriptBlock(AsyncFileUpload1, AsyncFileUpload1.GetType(), "img",
    "top.document.getElementById('imgUpload').src='image.jpg';", 
    true);
}

For Details on how to show the preview take a look at this example: AJAX File Upload in ASP.NET with the AsyncFileUpload Control

愛放△進行李 2024-11-13 13:13:56

我认为“~”不适用于 HTML 控件。尝试将路径转换为实际路径并设置图像源。

I don't think "~" will work with HTML controls. Try converting the path to actual path and set the Image src.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文