我写的下载文件代码怎么没有后缀名啊..
public ActionResult DownLoad() {
int id =Convert.ToInt32(Session["id"]);
Article article = resourceMangerServices.GetArticleById(id);
string directory = Server.MapPath("/FileUpdate");
string FilePath = directory + article.Attachment;
System.IO.FileInfo DownloadFile = new System.IO.FileInfo(FilePath);
Response.Clear();
Response.ContentType = "application/octet-stream ";
Response.AppendHeader("Content-Disposition ", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
return new EmptyResult();
} 高手 求解
------解决方案--------------------定义个处理程序
<a href="/DownloadHandler.ashx?filename=111111">点击下载</a>
public class DownloadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = HttpContext.Current.Request;
HttpResponse Response = HttpContext.Current.Response;
string strDirectory = HttpContext.Current.Server.MapPath("~");
string strFilePath = strDirectory + context.Request.QueryString["filename"]; //真实文件地址
System.IO.FileInfo DownloadFile = new System.IO.FileInfo(strFilePath );
string strFileName = DownloadFile.Name;
byte[] datas = System.IO.File.ReadAllBytes(strFilePath);
Response.AddHeader("Content-Disposition", "attachment;filename= \"" + HttpUtility.UrlEncode(strFileName) + "\";");
Response.AddHeader("Content-Type", "text/html"); // 需修改
Response.OutputStream.Write(datas, 0, datas.Length);
}
public bool IsReusable
{
get
{
return false;
}
}
}
------解决方案--------------------C# code
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace Asiastar.NR.Ajax
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string id = context.Request["id"].ToString();//获取资源的编号
System.IO.Stream iStream = null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;
NRBLL.File bf = new Asiastar.NRBLL.File();
Guid guid = new Guid(id);
if (bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"] != null)//判断数据库路径是否存在
{
string filepath = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString();//获取资源完整路径 D:\资源文件\600cc139-14cf-448e-9e50-daa972d35e01.jpg
string Oidfilename = bf.FN_SerchPathByFileI