Why is the file not downloading?

2019-06-14 06:46发布

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:

enter image description hereenter image description here

1条回答
We Are One
2楼-- · 2019-06-14 07:29

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>
查看更多
登录 后发表回答