为什么文件没有下载? [英] Why is the file not downloading?

查看:181
本文介绍了为什么文件没有下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照,它不可能没有解决方法



代码

 < ext:LinkBut​​ton ID =lnkDownload Text =下载runat =server> 
< DirectEvents>
< Click OnEvent =lnkDownload_ClickSuccess =Ext.net.DirectMethods.Download({isUpload:true}); IsUpload = 真 >
< EventMask ShowMask =true/>
< / Click>
< / DirectEvents>
< / ext:LinkBut​​ton>


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:

解决方案

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>

这篇关于为什么文件没有下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆