代码之家  ›  专栏  ›  技术社区  ›  FREAKYHUNTER

为什么文件不下载?

  •  0
  • FREAKYHUNTER  · 技术社区  · 11 年前

    我跟着去了 this 链接以下载文件系统中的文件。除了我使用Ext.net链接按钮,在该按钮的单击事件中,我添加了相同的 下载 密码

    设计部分为:

    <ext:LinkButton ID="lnkDownload" Text="Download" runat="server">
            <DirectEvents>
                <Click OnEvent="lnkDownload_Click">
                    <EventMask ShowMask="true" />
                </Click>
            </DirectEvents>
        </ext:LinkButton>
    

    后面的代码是:

    protected void lnkDownload_Click(object s, DirectEventArgs e)
        { 
            string filePath = // Path to the file
            FileInfo file = new FileInfo(filePath);
    
            if (file.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                if (file.Extension == ".txt")
                    Response.ContentType = "application/txt";
                else if (file.Extension == ".jpg")
                    Response.ContentType = "image/jpg";
                else
                    Response.ContentType = "application/octet-stream";
                Context.Response.WriteFile(file.FullName);
                Response.End();
            }
            else
            {
                Response.Write("This file does not exist.");
            }
        }
    

    运行代码时,发生错误。为文本文件显示文本内容,并且可能为图像文件显示加密的图像内容。发生了什么?为什么文件不下载?请帮我解决。

    快照包括:

    enter image description here enter image description here

    1 回复  |  直到 7 年前
        1
  •  3
  •   Mahmoud Darwish    11 年前

    要使其工作,请将代码更改为以下内容:

    <ext:LinkButton ID="lnkDownload" Text="Download" runat="server">
            <DirectEvents>
                <Click OnEvent="lnkDownload_Click" IsUpload="true">
                    <EventMask ShowMask="true" />
                </Click>
            </DirectEvents>
    </ext:LinkButton>
    

    笔记 :我认为内容类型应始终为“应用程序/八位字节流”。

    编辑

    对于面罩,根据 Daniil from the Ext.NET Team , its not possible without a workaround .

    准则

    <ext:LinkButton ID="lnkDownload" Text="Download" runat="server">
            <DirectEvents>
                <Click OnEvent="lnkDownload_Click" Success="Ext.net.DirectMethods.Download({ isUpload : true });" IsUpload="true">
                    <EventMask ShowMask="true" />
                </Click>
            </DirectEvents>
    </ext:LinkButton>