为什么文件没有下载?

时间:2014-03-10 08:36:38

标签: c# asp.net download ext.net

我按照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 个答案:

答案 0 :(得分:3)

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

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

注意:我认为内容类型应始终为“application / octet-stream”。

修改

对于面具,根据Daniil from the Ext.NET Teamits 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>