首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

小弟我写的下载文件代码如何没有后缀名啊

2012-05-27 
我写的下载文件代码怎么没有后缀名啊..public ActionResult DownLoad() {int id Convert.ToInt32(Session

我写的下载文件代码怎么没有后缀名啊..
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_SerchPathByFileId(guid).Tables[0].Rows[0]["FileNam"].ToString();//旧文件名称                //string filename = System.IO.Path.GetFileName(filepath);//获取文件名称+后缀名            600cc139-14cf-448e-9e50-daa972d35e01.JPG                //int index = filepath.IndexOf(".");                //string filetype = filepath.Substring(index).ToLower();//后缀名                //string newfilename = Oidfilename;                //string filepath1 = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString().Substring(0,filepath.Length - 8);                try                {                    string fileName = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(Oidfilename));//解码(注意这里2层解码)                    Oidfilename = Oidfilename.Replace("+", "%20");  //将“+”替换成“空格”                    iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);                    dataToRead = iStream.Length;                    context.Response.ContentType = "application/octet-stream";                    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Oidfilename, System.Text.Encoding.UTF8));  //下载的时候下载原来的文件名称                    while (dataToRead > 0)                    {                        if (context.Response.IsClientConnected)                        {                            length = iStream.Read(buffer, 0, 10000);                            context.Response.OutputStream.Write(buffer, 0, length);                            context.Response.Flush();                            buffer = new Byte[10000];                            dataToRead = dataToRead - length;                        }                        else                        {                            dataToRead = -1;                        }                    }                }                catch (Exception ex)                {                    NR.Error.Log.LogType(ex.ToString());                }                finally                {                    if (iStream != null)                    {                        iStream.Close();                    }                }            }            else            {                NR.Error.Log.LogType("找不到文件!");            }        }        public bool IsReusable        {            get            {                return false;            }        }    }} 

热点排行