我有ajax AsyncFileUpload控件和图像控件。当通过上传器选择图像时,我在图像控件中显示它。 我有处理程序:
public class Handler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
if (context.Request.QueryString.Count != 0)
{
//Get the stored image and write in the response.
var storedImage = context.Session["STORED_IMAGE"] as byte[];
if (storedImage != null)
{
Image image = GetImage(storedImage);
if (image != null)
{
context.Response.ContentType = "image/jpeg";
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
}
private Image GetImage(byte[] storedImage)
{
var stream = new MemoryStream(storedImage);
return Image.FromStream(stream);
}
public bool IsReusable
{
get
{
return false;
}
}
}
在Aspx页面中:
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Always" runat="server">
<ContentTemplate>
<div class="FakeFileUpload">
<div class="FakeFileUploadDiv">
<asp:Image ID="imgPhoto" runat="server" ImageUrl="download.jpeg" alt="Employee Photo"
Height="150px" Width="150px" />
</div>
<cc11:AsyncFileUpload ID="AsyncFileUpload1" runat="server" ThrobberID="loader"
CssClass="FileUploadClass" OnClientUploadComplete="OnClientAsyncFileUploadComplete"
OnUploadedComplete="AsyncFileUpload1_UploadedComplete" Width="150" />
<asp:Image ID="loader" runat="server" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
脚本:
<script type="text/javascript">
function getRandomNumber() {
var randomnumber = Math.random(10000);
return randomnumber;
}
function OnClientAsyncFileUploadComplete(sender, args) {
var handlerPage = '<%= Page.ResolveClientUrl("Handler.ashx")%>';
var queryString = '?randomno=' + getRandomNumber();
var src = handlerPage + queryString;
var clientId = '<%=imgPhoto.ClientID %>';
document.getElementById(clientId).setAttribute("src", src);
}
</script>
这一切都运行正常,但是当我点击任何其他按钮时,图像控件中的内容将丢失。我希望当回发发生时,该图像仍然存在。
我从以下代码获得了代码: http://www.harinadh.com/2012/07/profile-picture-using-asyncfileupload.html
任何人都可以帮我这样做吗?
提前致谢。