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; } } }