zoukankan      html  css  js  c++  java
  • c#图片输出

    1:  Response.BinaryWrite() 其实就是和输出文字一样 只是图片是流的形式;

            delegate long myDel(int first, int second);
            FileStream fs;
            byte[] _byte;
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.ContentType = "image/jpeg";
                fs = new FileStream(Server.MapPath("~/file/") + HttpUtility.UrlDecode("你好.jpg"), FileMode.Open);
                _byte = new byte[fs.Length];
                IAsyncResult ir = fs.BeginRead(_byte, 0, (int)fs.Length, null, null);
                fs.Read(_byte, 0, fs.EndRead(ir));
                fs.Close();
                Response.BinaryWrite(_byte);
    
            }

    2:循环

            protected void Button1_Click(object sender, EventArgs e)
            {
                string filePath = Server.MapPath("~/file/") + HttpUtility.UrlDecode("你好.jpg");
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("你好.jpg", Encoding.UTF8));
                //FileInfo fi = new FileInfo(filePath);
                //Response.WriteFile(fi.FullName);
                //Response.End();
    
    
                int length = 0;
                byte[] arr = new byte[1024 * 1024 * 2];
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    length = fs.Read(arr, 0, arr.Length);
                }
                byte[] arrNew = new byte[length];
                System.Buffer.BlockCopy(arr, 0, arrNew, 0, length);
                Response.BinaryWrite(arrNew);
            }
            public static void ResponseFile(string filePath, HttpContext context, bool hasfileName)
            {
                Stream iStream = null;
                byte[] buffer = new Byte[10000];
                int length;
                long dataToRead;
                context.Response.ContentType = "application/octet-stream";
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath), Encoding.UTF8));
                using (iStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    dataToRead = iStream.Length;
                    while (dataToRead > 0)
                    {
                        if (context.Response.IsClientConnected)
                        {
                            length = iStream.Read(buffer, 0, 10000);
                            context.Response.OutputStream.Write(buffer, 0, length);
                            context.Response.Flush();
                            buffer = new Byte[10000];
                            dataToRead = dataToRead - length;
                        }
                        else
                        {
                            dataToRead = -1;
                        }
                    }
                }
  • 相关阅读:
    使用linux将一个服务器上的文件或者文件夹复制黏贴到另一个服务器上
    深度学习之常用linux命令总结
    python 操作MYSQL数据库
    MYSQL登录及常用命令
    mysql数据库可视化工具—Navicat Premium—安装与激活
    Mysql数据库的简单介绍与入门
    java 正则匹配int型
    js设置文本框只能输入数字
    Java数据抓取(一)
    Nodejs的多线程
  • 原文地址:https://www.cnblogs.com/y112102/p/3139574.html
Copyright © 2011-2022 走看看