zoukankan      html  css  js  c++  java
  • response下载文件 (转载)

    核心代码:

    DataSet ds = dBll.GetList("ID=" + ID);
    string docName = "a.doc";//文件名,
            byte[] file = (byte[])ds.Tables[0].Rows[0]["FContent"]; //   "FContent"类型为image
            string Type = checktype(docName);
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(docName, System.Text.Encoding.UTF8).Replace("+", "%20"));
            Response.AddHeader("Content-Length ", file.Length.ToString());
            Response.ContentType = Type;
            Response.BinaryWrite(file);//可以下载二进制文件,如数据库存的image
            //Response.WriteFile(fliePath);可以下载路径文件
              Response.End();
            Response.Clear();
     
    /// <summary>
    /// 根据文件的扩展名来获取对应的“输出流的HTTP MIME“类型
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    private string checktype(string filename)
    {
        string ContentType;
        switch (filename.Substring(filename.LastIndexOf(".")).Trim().ToLower())
        {
            case ".asf ":
                ContentType = "video/x-ms-asf ";
                break;
            case ".avi ":
                ContentType = "video/avi ";
                break;
            case ".doc ":
                ContentType = "application/msword ";
                break;
            case ".zip ":
                ContentType = "application/zip ";
                break;
            case ".xls ":
                ContentType = "application/vnd.ms-excel ";
                break;
            case ".gif ":
                ContentType = "image/gif ";
                break;
            case ".jpg ":
                ContentType = "image/jpeg ";
                break;
            case "jpeg ":
                ContentType = "image/jpeg ";
                break;
            case ".wav ":
                ContentType = "audio/wav ";
                break;
            case ".mp3 ":
                ContentType = "audio/mpeg3 ";
                break;
            case ".mpg ":
                ContentType = "video/mpeg ";
                break;
            case ".mepg ":
                ContentType = "video/mpeg ";
                break;
            case ".rtf ":
                ContentType = "application/rtf ";
                break;
            case ".html ":
                ContentType = "text/html ";
                break;
            case ".htm ":
                ContentType = "text/html ";
                break;
            case ".txt ":
                ContentType = "text/plain ";
                break;
            default:
                ContentType = "application/octet-stream ";
                break;
        }
        return ContentType;
    }

     HttpUtility.UrlEncode(docName, System.Text.Encoding.UTF8).Replace("+", "%20"));这个可以解决中文文件名中包含空格的问题,UrlEncode编码后会把文件名中的空格转换中+(+转换为%2b),但是浏览器是不能理 解加号为空格的,所以在浏览器下载得到的文件,空格就变成了加号;
    解决办法:UrlEncode 之后, 将 “+” 替换成 “%20″,因为浏览器将%20转换为空格

      
  • 相关阅读:
    543. Diameter of Binary Tree【Easy】【二叉树的直径】
    114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】
    Java实现蛇形矩阵
    215. Kth Largest Element in an Array【Medium】【找到第 k 大的元素】
    524. Longest Word in Dictionary through Deleting【Medium】【删除后得到的字典中的最长单词】
    141. Linked List Cycle【Easy】【判断链表是否存在环】
    88. Merge Sorted Array【Easy】【双指针-不用额外空间归并两个有序数组】
    680. Valid Palindrome II【Easy】【双指针-可以删除一个字符,判断是否能构成回文字符串】
    345. Reverse Vowels of a String【Easy】【双指针-反转字符串中的元音字符】
    633. Sum of Square Numbers【Easy】【双指针-是否存在两个数的平方和等于给定目标值】
  • 原文地址:https://www.cnblogs.com/kingsony/p/3258904.html
Copyright © 2011-2022 走看看