• 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;
                        }
                    }
                }
  • 相关阅读:
    C++继承 派生类中的内存布局(单继承、多继承、虚拟继承)
    Linux 共享库(动态库)
    虚幻4
    从头认识java-16.5 nio的数据转换
    JavaScript实现禁用键盘和鼠标的点击事件
    Codeforces Round #277.5 (Div. 2)部分题解
    iOS-WKWebView使用
    我学cocos2d-x (三) Node:一切可视化对象的祖先
    Android Studio右下角不显示当前branch名称
    Neo4j简单的样例
  • 原文地址:https://www.cnblogs.com/y112102/p/3139574.html
Copyright © 2020-2023  润新知