流知识的学习资料(-)

 

/// <summary>
      /// 利用文件流下载文件
      /// </summary>
      /// <param name="cc"></param>
      public static void down(string cc)
      {
          if (cc != "")
          {

              string path = System.Web.HttpContext.Current.Server.MapPath(cc);

              System.IO.FileInfo file = new System.IO.FileInfo(path);

              if (file.Exists)
              {

                  System.Web.HttpContext.Current.Response.Clear();

                  System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

                  System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());

                  System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
                  System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

                  System.Web.HttpContext.Current.Response.Filter.Close();
                   System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" +cc);
                  


                  System.Web.HttpContext.Current.Response.WriteFile(file.FullName);


                  System.Web.HttpContext.Current.Response.End();

              }
              else
              {
                  System.Web.HttpContext.Current.Response.Write("文件不存在");
                  System.Web.HttpContext.Current.Response.End();
              }

          }
      }

      /// <summary>
      /// 进行防盗链处理 ,0为下载,其余显示图片
      /// </summary>
      /// <param name="file"></param>
      /// <param name="type"></param>
      public static void down(string file, int type)
      {
          if (System.Web.HttpContext.Current.Request.UrlReferrer == null)
          {
     
            System.Web.HttpContext.Current.Response.ContentType = "image/gif";
             System.Web.HttpContext.Current.Response.WriteFile("/images/daolian.gif");
          }
          else if (System.Web.HttpContext.Current.Request.UrlReferrer.ToString().IndexOf(System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"]) < 0)
          {
              System.Web.HttpContext.Current.Response.Write("2");
             System.Web.HttpContext.Current.Response.ContentType = "image/gif";
             System.Web.HttpContext.Current.Response.WriteFile("/images/daolian.gif");

          }
          else
          {
              if (type == 0)
              {
                  down(file);
              }
              else
              {

                  System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
                  System.Web.HttpContext.Current.Response.WriteFile(file);
              }
          }
      }

你可能感兴趣的:(server,String,File,null,Path)