首页 新闻 会员 周边 捐助

response 下载文件

0
悬赏园豆:5 [已解决问题] 解决于 2008-10-23 16:57

问题描述:asp.net2.0
    有一个如下格式的excel模板

[1,1] [1,2] [1,3]
[2,1] [2,2] [2,3]
[3,1] [3,2] [3,3]

当用户点击下载的时候,我就把模板中的[n,n]格式的数据替换成数据库中的数据后下载

我是这样的做的

用StreamReader sr=new StreamReader(filepath),
一次读取一行,
sr.readLine();
然后替换其中的[n,n]

读取完后
response.SetContentType("");
response.write("");
response.end();

出现错误:
   下载的excel中是乱码

求正确的做法

readonly的主页 readonly | 菜鸟二级 | 园豆:406
提问于:2008-10-21 17:28
< >
分享
最佳答案
0

excel 是二进制文件,不是文本文件,建议以二进制方式读取并发送给浏览器,不要用 ReadLine 这种方式读,否则肯定会有乱码。

eaglet | 专家六级 |园豆:17139 | 2008-10-22 06:59
其他回答(3)
0

设置了编码格式了吗?

Fencer | 园豆:7 (初学一级) | 2008-10-21 20:31
0

你可以试试这个:

StreamReader   read = new  StreamReader(@"x:\*.xx",System.Text.Encoding.GetEncoding("GB2312"));

RicoRui | 园豆:3663 (老鸟四级) | 2008-10-21 22:14
0

用下面的方法:  ResponseFile(下载后的文件名,要下载的文件全路径,允许的下载速度),很方便。

 

 

/// <summary>
    /// 输出硬盘文件,提供下载
    /// </summary>  
    /// <param name="_fileName">下载文件名</param>
    /// <param name="_fullPath">带文件名下载路径</param>
    /// <param name="_speed">每秒允许下载的字节数</param>
    /// <returns>返回是否成功</returns>
    public static bool ResponseFile(string _fileName, string _fullPath, long _speed)
    {
        HttpRequest  _Request = System.Web.HttpContext.Current.Request;
        HttpResponse  _Response = System.Web.HttpContext.Current.Response;
        try
        {
            FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            BinaryReader br = new BinaryReader(myFile);
            try
            {
                _Response.AddHeader("Accept-Ranges", "bytes");
                _Response.Buffer = false;
                long fileLength = myFile.Length;
                long startBytes = 0;

                int pack = 10240; //10K bytes
                //int sleep = 200;   //每秒5次   即5*10K bytes每秒
                int sleep = (int)Math.Floor((decimal)1000 * pack / _speed) + 1;
                if (_Request.Headers["Range"] != null)
                {
                    _Response.StatusCode = 206;
                    string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
                    startBytes = Convert.ToInt64(range[1]);
                }
                _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
                if (startBytes != 0)
                {
                    _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
                }
                _Response.AddHeader("Connection", "Keep-Alive");
                _Response.ContentType = "application/octet-stream";
                _Response.Charset = "UTF-8";
                _Response.ContentEncoding = Encoding.UTF8;
                _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, Encoding.UTF8));

                br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                int maxCount = (int)Math.Floor((decimal)(fileLength - startBytes) / pack) + 1;

                for (int i = 0; i < maxCount; i++)
                {
                    if (_Response.IsClientConnected)
                    {
                        _Response.BinaryWrite(br.ReadBytes(pack));
                        Thread.Sleep(sleep);
                    }
                    else
                    {
                        i = maxCount;
                    }
                }
                _Response.End();
            }
            catch
            {
                return false;
            }
            finally
            {
                br.Close();
                myFile.Close();
            }

        }
        catch (Exception err)
        {
            return false;
        }
        return true;
    }

张荣华 | 园豆:2020 (老鸟四级) | 2008-10-22 15:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册