代码如下,
  
Response.AddHeader("Content-Type", "application/octet-stream"); 
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(af.FileName, System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", af.Content.Length.ToString());
Response.BinaryWrite(af.Content);
Response.End();    
其中可从af对象中获得文件名和流数据。
在下载对话框中,点击“保存”一切正常,对于文本图片等可识别类型的文件,点击“打开”也正常,如果是.exe,.dll等文件,点击“打开”就会再次弹出下载对话框,再点击“打开”才打开文件,究竟是为什么呀!!非常郁闷着急!!

解决方案 »

  1.   

    Try to comment code below,and try again.
    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(af.FileName, System.Text.Encoding.UTF8));
      

  2.   

    or try to this:
    Response.AddHeader("Content-Disposition", "inline; filename=" + HttpUtility.UrlEncode(af.FileName, System.Text.Encoding.UTF8));
      

  3.   

    是不是你的.exe文件会自动下载
      

  4.   

    /// </summary>
    public void downloadFile(HttpRequest reqst,HttpResponse respon, HttpServerUtility server ,  string userId,string nodeId,string docId,string version,bool isCheckOut)
    {
    DataBase db = null;
    System.IO.FileStream myFile = null;
    //BinaryReader br = null;
    respon.Clear(); try
    {
    db= new DataBase();
    db.Open(); // Check the File
    this.checkFileExisted( db.Connection , null , nodeId , docId , null );
    }
    catch( ApplicationException ex )
    {
    throw ex;
    }
    catch( Exception ex )
    {
    throw ex;
    }
    finally
    {
    db.Close();
    } try
    {
    db= new DataBase();
    db.Open(); // DownLoad File
    string  sqlText  = " SELECT DocName, Content ";
    sqlText += " FROM   T_DMS_DOC ";
    sqlText += " WHERE  NodeId  ='"+DataBase.SqlItemTextChange(nodeId)+"' ";
    sqlText += " AND    DocId   ='"+DataBase.SqlItemTextChange(docId)+"' ";
    sqlText += " AND    Version ='"+DataBase.SqlItemTextChange(version)+"' ";

    SqlCommand cmd=new SqlCommand(sqlText,db.Connection);
    SqlDataReader rd;
                
    // Write Log
    LogManager.WriteLog("Execute  "+cmd.CommandText);
    rd=cmd.ExecuteReader(); while(rd.Read())
    {
    // Orginal File Name
    string fileName = rd["DocName"].ToString(); // Temp File Name with server path
    string ServerPath = server.MapPath("TempFile"); // Delete temp , if existed
    if(File.Exists(ServerPath))
    {
    File.Delete(ServerPath);
    } // Get File from database
    myFile = new System.IO.FileStream(ServerPath,System.IO.FileMode.Create);
    myFile.Write(((byte[])rd["Content"]),0,((byte[])rd["Content"]).Length); #region New Source
    // Write file into harddisk
    StreamWriter swMyFile = new StreamWriter( myFile ) ;
    swMyFile.WriteLine("") ;
    swMyFile.Flush() ;
    swMyFile.Close() ;
    myFile.Close() ; // Get Temp File info
    FileInfo fi = new FileInfo(ServerPath); // determine the name and size of the file
    string filename = fi.Name;
    string filesize = fi.Length.ToString();
    fi = null; // clear the response, set the contenttype and the file name/size
    respon.Clear();
    respon.ContentType = "application/octet-stream";
    respon.AppendHeader("Content-Disposition", "attachment; filename=" + server.UrlEncode(fileName) + @"");
    respon.AppendHeader ("Content-Length", filesize);  // flush the response, then send the file down
    respon.Flush();
    respon.WriteFile(ServerPath);
    #endregion #region Old Source
    // br = new BinaryReader(myFile);
    //
    // //respon.Clear();
    // respon.AddHeader("Accept-Ranges", "bytes");
    // respon.Buffer = false;
    // long fileLength = myFile.Length;
    // long startBytes = 0;
    //     
    // int pack = 10240; //10K bytes
    // //int sleep = 200;   
    // int sleep = (int)Math.Floor(1000 * pack / 1048576) + 1;
    // if (reqst.Headers["Range"] != null)
    // {
    // respon.StatusCode = 206;
    // string[] range = reqst.Headers["Range"].Split(new char[] {'=', '-'});
    // startBytes = Convert.ToInt64(range[1]);
    // }
    // respon.AddHeader("Content-Length", (fileLength - startBytes).ToString());
    // if (startBytes != 0)
    // {
    // respon.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
    // }
    //
    // respon.AddHeader("Connection", "Keep-Alive");
    // respon.ContentType = "application/octet-stream";
    // respon.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8).Replace("+"," ") );
    // 
    // br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
    // int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
    //
    // respon.Flush();
    //
    // for (int i = 0; i < maxCount; i++)
    // {
    // if (respon.IsClientConnected)
    // {
    // respon.BinaryWrite(br.ReadBytes(pack));
    // Thread.Sleep(sleep);
    // }
    // else
    // {
    // i=maxCount; 
    // }
    // }
    //
    // br.Close();
    // myFile.Close();
    #endregion
    } rd.Close();
    db.Close();
    }
    catch(ApplicationException ex)
    {
    throw ex;
    }
    catch(Exception ex)
    {
    throw ex;
    }
    finally
    {
    // db.Close();
    respon.End();
    }

    }
      

  5.   

    看这里,我的解决方法:
    http://community.csdn.net/Expert/topic/4139/4139929.xml?temp=.797558