代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<%@ import namespace="System.IO" %>
<%@ import namespace="System.Drawing" %>
<%@ import namespace="System.Drawing.Imaging" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/C#" runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Files.Count>0)
{
//Get file in byte format
HttpPostedFile f = Request.Files[0];
byte[] buf = new byte[f.ContentLength];
f.InputStream.Read(buf, 0, buf.Length);
MemoryStream ms = new MemoryStream(buf);
Bitmap map = new Bitmap(ms);
Response.Clear();
map.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End();
map.Dispose();
ms.Close();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:fileupload id="FileUpload1" runat="server" />
<asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" />
</div>
</form>
</body>
</html>
<%@ import namespace="System.IO" %>
<%@ import namespace="System.Drawing" %>
<%@ import namespace="System.Drawing.Imaging" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/C#" runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Files.Count>0)
{
//Get file in byte format
HttpPostedFile f = Request.Files[0];
byte[] buf = new byte[f.ContentLength];
f.InputStream.Read(buf, 0, buf.Length);
MemoryStream ms = new MemoryStream(buf);
Bitmap map = new Bitmap(ms);
Response.Clear();
map.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End();
map.Dispose();
ms.Close();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:fileupload id="FileUpload1" runat="server" />
<asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" />
</div>
</form>
</body>
</html>
IE客户端得到的源代码
IE客户端得到的结果
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<img src="http://localhost:25715/WebSite7/Default5.aspx"></body></html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<img src="http://localhost:25715/WebSite7/Default5.aspx"></body></html>
注意,客户端使用img的src属性定位服务器端输出图片流的页面