• 下载文件


    /// <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(); }

    }

    }

  • 相关阅读:
    【转】【WCF】WCF中客户端生成代理的两种方式
    【WPF】鼠标拖拽功能DragOver和Drop
    【转】【Android】Android不同版本下Notification创建方法
    【C#】获取电脑DPI
    【转】7Z命令行解压缩
    【转】【C#】ZIP、RAR 压缩与解压缩
    【MySQL】字符串截取之substring_index
    【转】【Java/Android】Intent的简介以及属性的详解
    友盟分享和cocos2dx符合重复duplicate symbol 解决方案
    slimphp中间件调用流程的理解
  • 原文地址:https://www.cnblogs.com/gfbppy/p/10229682.html
Copyright © 2020-2023  润新知