Why is the file not downloading?

梦想与她 提交于 2019-12-08 11:58:27

问题


I followed this link to download file that are in the file system. Except I did it using Ext.net link button, and in the click event of that button I added the same Download code.

The design part is:

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

And the codebehind is:

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.");
        }
    }

When running the code, error occurs. Text content is displayed for text file, and maybe the encrypted image content is displayed for image file. What is happening? Why is the file not downloading? Please help me how to solve.

The snapshots are:


回答1:


For it to work change the code to the following:

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

Note: I think the content type should always be "application/octet-stream".

Edit

For the Mask, as per Daniil from the Ext.NET Team, its not possible without a workaround.

The Code

<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>


来源:https://stackoverflow.com/questions/22295384/why-is-the-file-not-downloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!