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, , (int)fs.Length, null, null);
fs.Read(_byte, , 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 = ;
byte[] arr = new byte[ * * ];
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
length = fs.Read(arr, , arr.Length);
}
byte[] arrNew = new byte[length];
System.Buffer.BlockCopy(arr, , arrNew, , length);
Response.BinaryWrite(arrNew);
}
public static void ResponseFile(string filePath, HttpContext context, bool hasfileName)
{
Stream iStream = null;
byte[] buffer = new Byte[];
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 > )
{
if (context.Response.IsClientConnected)
{
length = iStream.Read(buffer, , );
context.Response.OutputStream.Write(buffer, , length);
context.Response.Flush();
buffer = new Byte[];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -;
}
}
}