zoukankan      html  css  js  c++  java
  • Extjs.net Button点击下载jpg图片

    <ext:Button ID="DownLoad" runat="server" Text="下载二维码" Width="120" Height="35"
        AutoPostBack="false">
        <DirectEvents>
            <Click OnEvent="DownLoad_Click">
            </Click>
        </DirectEvents>
    </ext:Button>
    protected void DownLoad_Click(object sender, DirectEventArgs e)
    {
        WriteFile("/images/1.jpg");
    }
    public void WriteFile(string filePath)
    {
    
        try
        {
            string _pre_path = filePath;
            filePath = Server.MapPath(filePath);
            if (File.Exists(filePath))
            {
                FileInfo info = new FileInfo(filePath);
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(filePath));
                Response.AddHeader("Content-Length", info.Length.ToString());
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.ContentType = "application/octet-stream";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.WriteFile(info.FullName);
                Response.Flush();
                Response.End();
    
            }
        }
        catch (System.Threading.ThreadAbortException ex0) { }
        catch (Exception ex1)
        { }
        finally
        {
            HttpContext.Current.Response.Close();
        }
    }

    如果使用以上代码会报200错误

    解决方法:

    1、在Button上加一个FileUploadField就能正常运行,很莫名其妙,1.2版本,4.6版本都有这个现象。

    <ext:FileUploadField ID="fileUpload" runat="server" ButtonOnly="true" EmptyText="上传LOGO"
        ButtonText="">
    </ext:FileUploadField>
    <ext:Button ID="DownLoad" runat="server" Text="下载二维码" Width="120" Height="35"
        AutoPostBack="false">
        <DirectEvents>
            <Click OnEvent="DownLoad_Click">
            </Click>
        </DirectEvents>
    </ext:Button>

    2、直接使用OnClick加上AutoPostBack=true可以,但是这种方式页面会刷新一下,另外手动刷新页面时有时会触发下载事件。如果使用Mask的话,必须加AutoPostBack=true,不然Mask不会消失。

    3、最近同事发现的方法<DirectEvents>下加一个IsUpLoad="true"属性。

  • 相关阅读:
    纹理加载和异步
    地板上创建批量小方块
    创建自定义几何体(以立方体为例)
    WTForms
    angular和vue的差别
    vuejs简单介绍特点
    angularjs简单介绍和特点
    flask重要点
    redis
    DRF之认证组件源码解析
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/9287127.html
Copyright © 2011-2022 走看看