ASHX 图像下载另存为 ASHX

发布于 2024-10-30 15:51:55 字数 937 浏览 5 评论 0原文

当用户单击我的链接时,它会弹出“另存为”对话框,但它希望在 IE 以外的任何其他浏览器中将其另存为 ashx 文件。该文件是从数据库中获取的标准 jpeg。什么会导致这种情况发生?

       string id = ctx.Request.QueryString["id"];

        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand("EBig", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@EID", id);

        byte[] pict = null;
        ctx.Response.ContentType = "image/pjpeg";
        try
        {
            con.Open();
            pict = (byte[])cmd.ExecuteScalar();

            ctx.Response.Clear();

            ctx.Response.AppendHeader("content-disposition", "attachment;");
            ctx.Response.ContentType = "image/pjpeg";
            ctx.Response.BinaryWrite(pict);
            ctx.Response.Flush();
            ctx.Response.End();
        }
        catch
        {

        }
        finally
        {
            con.Close();
        }

When a user clicks my link it brings up the save as dialog, but it wants to save it as a ashx file in any other browser than IE. The file is standard jpeg that is being taken from a database. What would cause this to do?

       string id = ctx.Request.QueryString["id"];

        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand("EBig", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@EID", id);

        byte[] pict = null;
        ctx.Response.ContentType = "image/pjpeg";
        try
        {
            con.Open();
            pict = (byte[])cmd.ExecuteScalar();

            ctx.Response.Clear();

            ctx.Response.AppendHeader("content-disposition", "attachment;");
            ctx.Response.ContentType = "image/pjpeg";
            ctx.Response.BinaryWrite(pict);
            ctx.Response.Flush();
            ctx.Response.End();
        }
        catch
        {

        }
        finally
        {
            con.Close();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水波映月 2024-11-06 15:51:55

Hanselman 对此有一篇文章。它涉及在 Content-Disposition 响应标头上设置名为 filename 的附加属性。

相关的RFC在这里,相关部分是2.3:

如果实体是,发件人可能想要建议使用的文件名
分离并存储在单独的文件中。如果接收 MUA 写入
文件的实体,建议的文件名应用作
如果可能的话,以实际文件名为基础。

Hanselman has a post on this. It involves setting an additional attribute named filename on your Content-Disposition response header.

The related RFC is here, relevant section is 2.3:

The sender may want to suggest a filename to be used if the entity is
detached and stored in a separate file. If the receiving MUA writes
the entity to a file, the suggested filename should be used as a
basis for the actual filename, where possible.

爱你是孤单的心事 2024-11-06 15:51:55

您需要添加文件名:

context.Response.AppendHeader("Content-Disposition",
    string.Format("attachment;filename={0}", yourFileName));

You need to add the filename:

context.Response.AppendHeader("Content-Disposition",
    string.Format("attachment;filename={0}", yourFileName));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文