asp.net 直接弹出保存文件,而不是打开文件
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
public partial class Default4 : System.Web.UI.Page
{
protected String _Content = "";//出错提示
protected void Page_Load(object sender, EventArgs e)
{
//在ASP.NET中有时候需要点击一个文件的链接将其下载而不是直接在浏览其中打开该文件,则可以采用如下处理:
if (!IsPostBack)
{
string fileName = Request.QueryString["FileName"];//传过来的文件名称如:aaa.jpg
if (fileName != null && fileName.Trim() != "")
{
String path1 = Server.MapPath("/Image/Practice/" + HttpUtility.UrlEncode(fileName));
FileInfo fi_delete1 = new FileInfo(path1);
if (fi_delete1.Exists)//判断文件是否存在
{
string filePath = System.Configuration.ConfigurationManager.AppSettings["ArticleUploadPath"];
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));//保存时候的文件名
Response.WriteFile(Server.MapPath("/Image/Practice/" + fileName));//输出的文件路径
Response.End();
}
else
{
_Content = "<br/>练习文件不存在,请与客服联系。";
}
}
}
//中文名文件的时候需要如上面的HttpUtility.UrlEncode
}
}
下面是输出中文不会乱码的程序:支持ie浏览器
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text;
public partial class Default4 : System.Web.UI.Page
{
protected String _Content = "";//出错提示
protected void Page_Load(object sender, EventArgs e)
{
//在ASP.NET中有时候需要点击一个文件的链接将其下载而不是直接在浏览其中打开该文件,则可以采用如下处理:
if (!IsPostBack)
{
string fileName = Request.QueryString["FileName"];
if (fileName != null && fileName.Trim() != "")
{
String path1 = Server.MapPath(HttpUtility.UrlEncode(fileName));
FileInfo fi_delete1 = new FileInfo(path1);
if (fi_delete1.Exists)//判断文件是否存在
{
string filePath = System.Configuration.ConfigurationManager.AppSettings["ArticleUploadPath"];
Response.Clear();
Response.ContentType = "application/octet-stream";
String fileName11 = ToHexString("v2.4(全部高校)WBYY对口高校资助方案和申请表.pdf");
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName11);
//Response.AddHeader("Content-Disposition", "attachment;filename=v2.4WBYY_GaoXiaoZiZhuFangAnHeShenQingBiao.pdf");
Response.WriteFile(Server.MapPath(fileName));
Response.End();
}
else
{
_Content = "<br/>文件不存在,请与客服联系。";
}
}
}
//中文名文件的时候需要如上面的HttpUtility.UrlEncode
}
/// <summary>
/// 为字符串中的非英文字符编码
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ToHexString(string s)
{
char[] chars = s.ToCharArray();
StringBuilder builder = new StringBuilder();
for (int index = 0; index < chars.Length; index++)
{
bool needToEncode = NeedToEncode(chars[index]);
if (needToEncode)
{
string encodedString = ToHexString(chars[index]);
builder.Append(encodedString);
}
else
{
builder.Append(chars[index]);
}
}
return builder.ToString();
}
/// <summary>
///指定 一个字符是否应该被编码
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static bool NeedToEncode(char chr)
{
string reservedChars = "$-_.+!*'(),@=&";
if (chr > 127)
return true;
if (char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr) >= 0)
return false;
return true;
}
/// <summary>
/// 为非英文字符串编码
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static string ToHexString(char chr)
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] encodedBytes = utf8.GetBytes(chr.ToString());
StringBuilder builder = new StringBuilder();
for (int index = 0; index < encodedBytes.Length; index++)
{
builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
}
return builder.ToString();
}
}

浙公网安备 33010602011771号