protected void btnDownLoad_Click(object sender, EventArgs e)
        {
            System.IO.Stream iStream = null;            // Buffer to read 10K bytes in chunk:
            byte[] buffer = new Byte[5000000];            // Length of the file:
            int length;            // Total bytes to read:
            long dataToRead;            // Identify the file to download including its path.
            //string filepath = "DownloadFileName";
            string filepath = Server.MapPath("./") + "UploadedFile\\" + this.ddlInstallType.SelectedValue + "\\" + this.ddlCarType1.SelectedValue + this.ddlCarType2.SelectedValue + "-" + this.ddlCarNumber.SelectedValue;
            // Identify the file name.
            string filename = txtFileName.Text.Trim();
            string strFilepath = filepath + "\\" + filename;
            try
            { 
                if (!File.Exists(strFilepath))
                {
                    this.lblErrorMessage.Text = "该文件不存在.";
                    return;
                }
                // Open the file.
                iStream = new System.IO.FileStream(strFilepath, System.IO.FileMode.Open,
                System.IO.FileAccess.Read, System.IO.FileShare.Read);
                // Total bytes to read:
                dataToRead = iStream.Length;                Response.ContentType = "application/octet-stream;charset=gb2321";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename));
                // Read the bytes.
                while (dataToRead > 0)
                {
                    // Verify that the client is connected.
                    if (Response.IsClientConnected)
                    {
                        // Read the data in buffer.
                        length = iStream.Read(buffer, 0, 10000);                        // Write the data to the current output stream.
                        Response.OutputStream.Write(buffer, 0, length);                        // Flush the data to the HTML output.
                        Response.Flush();                        buffer = new Byte[5000000];
                        dataToRead = dataToRead - length;
                    }
                    else
                    {
                        //prevent infinite loop if user disconnects
                        dataToRead = -1;
                    }
                }
            }
            catch
            {
                // Trap the error, if any.
                this.lblErrorMessage.Text = "下载失败.";
            }
            finally
            {
                if (iStream != null)
                {
                    //Close the file.
                    iStream.Close();
                }
            }
        }Q:例如我要下载后缀名为.txt的文件,为什么下载的文件连<html>这些标签都写进去了。谁能告诉我这段代码,如何修改能不写入 html标签元素

解决方案 »

  1.   

    删除前台ASPX页面的所有HTML内容。
    另外你的BUFFER定义得太大了,一般都是1024就行了
      

  2.   

    要是把前台aspx的内容都删了。
    我还提个什么问阿。next
      

  3.   

    哦,一般都是采用1个单独的ASPX页面来输出文件流的,你的按钮就只提供下转向而已。你要在本页输出的话,估计不好整哟。
    比如你的DOWNLOAD的按钮,就传个参数给专门处理文件流输出的页面。那个页面的Aspx为空白的。就行了
      

  4.   

    7楼脑残,不解释。
    我都怀疑了 干IT的 怎么出来你这么个货色。楼下看清楚, 我aspx前台的东西是必须的,不能删,要是删了,我还在这发什么贴。
    搞笑
      

  5.   


    没有让你删啊!!服务器发给浏览器的都是数据流,你前台的ASPX页面的HTML这个也是数据流的一部分。所以你如果非要本页输出这个文件流,那么HTML的数据肯定和这个文件流一起通过outstream输出来的,所以你的txt里面就肯定有HTML的内容。所以让你变通下。你本页的按钮就做转向处理,转向到另一个专门处理输出文件流的空白的ASPX或者Ashx页面。(而且这样做也更加便于维护)不知道你能理解不呢?
      

  6.   

    呵呵,本来CSDN上就只能解决简单的问题。