zoukankan      html  css  js  c++  java
  • 下载文件

    /// <summary>
    /// 下载服务器本地路径文件
    /// </summary>
    /// <param name="filePath">相对路径</param>
    public void WriteFile(string filePath)
    {
    try
    {
    string _pre_path = filePath;
    filePath = Server.MapPath(filePath);
    if (File.Exists(filePath))
    {
    FileInfo info = new FileInfo(filePath);
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "attachment;filename=erweima.jpg");
    Response.AddHeader("Content-Length", info.Length.ToString());
    Response.AddHeader("Content-Transfer-Encoding", "binary");
    Response.ContentType = "application/octet-stream";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    Response.WriteFile(filePath);
    Response.Flush();
    Response.End();

    }
    }
    catch (System.Threading.ThreadAbortException ex0) { }
    catch (Exception ex1)
    { }
    finally
    {
    HttpContext.Current.Response.Close();
    }
    }

    /// <summary>
    /// 下载网络路径图片
    /// </summary>
    /// <param name="url">网络路径</param>
    public void WriteFileUrl(string url)
    {


    Bitmap img = null;
    System.Uri httpUrl = new System.Uri(url);
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(httpUrl);

    req.ServicePoint.Expect100Continue = false;

    req.Method = "GET";

    req.KeepAlive = true;

    req.ContentType = "image/*";

    HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();

    System.IO.Stream stream = null;
    System.IO.MemoryStream ms = new MemoryStream();

    try
    {

    stream = rsp.GetResponseStream();
    img = new Bitmap(rsp.GetResponseStream());
    //img.Save(@"E:/" + DateTime.Now.ToFileTime().ToString() + ".png");
    img.Save(ms, ImageFormat.Png);

    HttpContext curContext = HttpContext.Current;
    curContext.Response.ContentType = "application/octet-stream";
    curContext.Response.ContentEncoding = Encoding.UTF8;
    curContext.Response.Charset = "";
    curContext.Response.AppendHeader("Content-Disposition",
    "attachment;filename=" + HttpUtility.UrlEncode("erweimaqiye.jpg", Encoding.UTF8));
    curContext.Response.BinaryWrite(ms.ToArray());
    curContext.Response.End();

    }
    catch (System.Threading.ThreadAbortException ex0)
    { }
    catch (Exception ex1)
    { }
    finally
    {

    if (stream != null) { stream.Dispose(); stream.Close(); }

    if (rsp != null) { rsp.Close(); }

    if (ms != null) { ms.Dispose(); ms.Close(); }

    }

    }

  • 相关阅读:
    绕口令系列 1
    毕业论文排版
    使用matlab表示“段数不确定”的分段函数
    [转]C/C++关于全局变量和局部变量初始化与不初始化的区别
    [转]基于Protel DXP软件的PCB高级编辑技巧大全
    冒泡排序及其优化
    gcc编译器参数
    [转]跟我一起写Makefile系列
    实例说明optimize table在优化mysql时很重要
    log4php0.9的详细配备实例说明
  • 原文地址:https://www.cnblogs.com/gfbppy/p/10229682.html
Copyright © 2011-2022 走看看