强制另存文件和加扩展名的代码c#

强制另存为文件+扩展名的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AspDotNet03
{
/// <summary>
/// C11Down 负责 读取请求文件并 提示浏览器 另存为
/// </summary>
public class C11Down : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//1.获取要下载的文件路径
string strFilePath = context.Request.QueryString["n"];
//2.转成物理路径
strFilePath = context.Request.MapPath(strFilePath);

//关键:添加一个相应报文头,强制浏览器 以另存为附件 的方式 打开 本次输出的响应报文
context.Response.AddHeader("Content-Disposition", "attachment;filename=gzitcast.jpg");


//3.将文件读取并输出给浏览器
context.Response.WriteFile(strFilePath);
}

public bool IsReusable
{
get
{
return false;
}
}
}
}

原文地址:https://www.cnblogs.com/kexb/p/3660391.html