如题

解决方案 »

  1.   

    /// <summary>
    /// 文件下载
    /// </summary>
    /// <param name="FullFileName"></param>
    private void FileDownload(string FullFileName)
    {
    FileInfo DownloadFile = new FileInfo(FullFileName); 
    Response.Clear();
    Response.ClearHeaders();
    Response.Buffer=false;
    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();
    }
      

  2.   

    1.可以直接连接到文件上,但是有的时候没有保存,只是在浏览器中打开(像word,excel等)
    2.可以使用代码实现下载文件出现提示框或者直接显示在浏览器中 
    出现提示框string strFile="F:\\a.doc";//路径根据实际情况而定
    if(!System.IO.File.Exists(strFile))
       {
        Response.Write("<script language='javascript'>alert('对不起,文件不存在!');</script>");
        return;
       }
       Response.Clear();
       Response.ClearHeaders();
       Response.Charset = "GB2312";
       Response.ContentEncoding =System.Text.Encoding.UTF8;
       Response.ContentType = "application/octet-stream"; 
       FileInfo fi=new FileInfo(strFile);
       Response.AddHeader("Content-Disposition","attachment;  filename="  +  HttpUtility.UrlEncode(fi.Name)) ;
       Response.AddHeader("Content-Length",fi.Length.ToString());
       byte[] tmpbyte=new byte[1024*8];
       FileStream fs=fi.OpenRead();
       int count;
       while((count=fs.Read(tmpbyte,0,tmpbyte.Length))>0)
       {
        Response.BinaryWrite(tmpbyte);
        Response.Flush();
       }
       fs.Close();   
       Response.End();直接在浏览器中打开
       string strFile="F:\\a.doc";//路径根据实际情况而定
       Response.Clear();
       Response.ClearHeaders();
       Response.Charset = "GB2312";
       Response.ContentEncoding =System.Text.Encoding.UTF8;
       Response.ContentType = "application/msword"; 
       Response.WriteFile(strFile);
      

  3.   

    不知你的文件链接是用<a></a>还是用别的,如果用这个的话,把href属性指向你的文件所在的路径就可以了,如果用别的也一样,将相应属性指向文件路径。
      

  4.   

    我的苦恼在于,点击“打开”后文件就在当前的页面中打开,而如果在链接后加了"target='_blank'"项双会在新页面中直接打开,点击左键时又不能下载,有没有简便点的方法让点击“打开”时不在当前页面打开文档,我不想在IE中出现word啊,WuWu.....
      

  5.   

    我已经说了阿,这些都是客户端问题,你控制不了的。
    你可以改变文件的扩展名,如doc改为 docc
      

  6.   

    在不在浏览器中打开,应该是与浏览器对各类文件关联设置有关.如果浏览器认为自己打不开,它就会调用相应的程序打开文件,如果没有相应的程序,就会出现下载框.
    而这些设置需要在客户端进行.
    所以,你只能告诉用户,想要直接在WORD里面打开文件,最好直接下载到本地,然后再打开,不要在网页里面直接点击.
      

  7.   

    将URL地址设置为你要下载的文件地址就可以了,
      

  8.   

    ASP.NET直接下载一个文件,而不是在IE中打开它。可用下面代码来替换Response.RedirectResponse.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment;FileName="+YourFileName);
    Response.BinaryWrite((byte[])YourFileData.Rows[0]["AttachmentContent"]);
    Response.End();