前台WebForm1.aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Web.User.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<br> function $(id) {<br> return document.getElementById(id);<br> }<br> var userAgent = navigator.userAgent.toLowerCase();<br> var is_opera = userAgent.indexOf('opera') != -1 && opera.version();<br> var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);<br> var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);<br> var is_mac = userAgent.indexOf('mac') != -1;<br> function AC_GetArgs(args, classid, mimeType) {<br> var ret = new Object();<br> ret.embedAttrs = new Object();<br> ret.params = new Object();<br> ret.objAttrs = new Object();<br> for (var i = 0; i < args.length; i = i + 2) {<br> var currArg = args[i].toLowerCase();<br> switch (currArg) {<br> case "classid": break;<br> case "pluginspage": ret.embedAttrs[args[i]] = '<a href="http://www.macromedia.com/go/getflashplayer">http://www.macromedia.com/go/getflashplayer</a>'; break;<br> case "src": ret.embedAttrs[args[i]] = args[i + 1]; ret.params["movie"] = args[i + 1]; break;<br> case "codebase": ret.objAttrs[args[i]] = '<a href="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0">http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0</a>'; break;<br> case "onafterupdate": case "onbeforeupdate": case "onblur": case "oncellchange": case "onclick": case "ondblclick": case "ondrag": case "ondragend":<br> case "ondragenter": case "ondragleave": case "ondragover": case "ondrop": case "onfinish": case "onfocus": case "onhelp": case "onmousedown":<br> case "onmouseup": case "onmouseover": case "onmousemove": case "onmouseout": case "onkeypress": case "onkeydown": case "onkeyup": case "onload":<br> case "onlosecapture": case "onpropertychange": case "onreadystatechange": case "onrowsdelete": case "onrowenter": case "onrowexit": case "onrowsinserted": case "onstart":<br> case "onscroll": case "onbeforeeditfocus": case "onactivate": case "onbeforedeactivate": case "ondeactivate": case "type":<br> case "id": ret.objAttrs[args[i]] = args[i + 1]; break;<br> case "width": case "height": case "align": case "vspace": case "hspace": case "class": case "title": case "accesskey": case "name":<br> case "tabindex": ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i + 1]; break;<br> default: ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i + 1];<br> }<br> }<br> ret.objAttrs["classid"] = classid;<br> if (mimeType) {<br> ret.embedAttrs["type"] = mimeType;<br> }<br> return ret;<br> }<br> function AC_FL_RunContent() {<br> var ret = AC_GetArgs(arguments, "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000", "application/x-shockwave-flash");<br> var str = '';<br> if (is_ie && !is_opera) {<br> str += '<object ';<br> for (var i in ret.objAttrs) {<br> str += i + '="' + ret.objAttrs[i] + '" ';<br> }<br> str += '>';<br> for (var i in ret.params) {<br> str += '<param name="' + i + '" value="' + ret.params[i] + '" /> ';<br> }<br> str += '</object>';<br> } else {<br> str += '<embed ';<br> for (var i in ret.embedAttrs) {<br> str += i + '="' + ret.embedAttrs[i] + '" ';<br> }<br> str += '></embed>';<br> }<br> return str;<br> }</p> <pre><code></script> </code></pre> <p></head><br> <body><br> <form id="form1" runat="server"><br> <table cellspacing="0" cellpadding="0" ><br> <tbody><br> <tr><br> <th></th><br> <td><br><br> <div class="avatararea"><br> <p><img id="avatar" onerror="this.onerror=null;this.src='../image/head.png';" /></p><br> <p><a href="javascript:;" onclick="$('avatarctrl').style.display = ''">正面</a><br> </p><br> </div><br> <div id="avatarctrl" style="display: none"><br> <script type="text/javascript"><br> document.write(AC_FL_RunContent('width', '540', 'height', '253', 'scale', 'exactfit', 'src', '<% =avatarFlashParam %>', 'id', 'mycamera', 'name', 'mycamera', 'quality', 'high', 'bgcolor', '#ffffff', 'wmode', 'transparent', 'menu', 'false', 'swLiveConnect', 'true', 'allowScriptAccess', 'always'));<br>
<br> function updateavatar(sender, args) {<br> $('avatar').src = '<%=Localhost %>/image/upload/idcardphoto/<%=uid %>/medium.jpg?random=1' + Math.random();<br> $('avatarctrl').style.display = 'none';<br> }<br> updateavatar();<br>
后台WebForm1.aspx.cs页面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web.User
{
public partial class WebForm1 : System.Web.UI.Page
{
protected string avatarFlashParam;
protected string EncodeLocalhost;
protected string Localhost;
protected string uid;
protected void Page_Load(object sender, EventArgs e)
{
int port = HttpContext.Current.Request.Url.Port;
string ApplicationPath = HttpContext.Current.Request.ApplicationPath != "/" ? HttpContext.Current.Request.ApplicationPath : string.Empty;
uid = "1";
Localhost = string.Format("{0}://{1}{2}{3}",
HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.Url.Host,
(port == 80 || port == 0) ? "" : ":" + port,
ApplicationPath);
EncodeLocalhost = HttpUtility.UrlEncode(Localhost);
avatarFlashParam = string.Format("{0}/image/common/camera.swf?nt=1&inajax=1&appid=1&input={1}&ucapi={2}/PhotoAjax.ashx", Localhost, uid, EncodeLocalhost);
}
}
}
PhotoAjax页面:
<%@ WebHandler Language="C#" Class="PhotoAjax" %>
using System;
using System.Web;
using System.IO;
public class PhotoAjax : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string uid = context.Request.QueryString["input"];
if (!string.IsNullOrEmpty(context.Request["Filename"]) && !string.IsNullOrEmpty(context.Request["Upload"]))
{
ResponseText(UploadTempAvatar(uid));
}
if (!string.IsNullOrEmpty(context.Request["avatar1"]) && !string.IsNullOrEmpty(context.Request["avatar2"]) && !string.IsNullOrEmpty(context.Request["avatar3"]))
{
CreateDir(uid);
if (!(SaveAvatar("avatar1", uid) && SaveAvatar("avatar2", uid) && SaveAvatar("avatar3", uid)))
{
File.Delete(GetMapPath("..\\image\\upload\\idcardphoto\\" + uid + ".jpg"));
ResponseText("<?xml version=\"1.0\" ?><root><face success=\"0\"/></root>");
return;
}
File.Delete(GetMapPath("..\\image\\upload\\idcardphoto\\" + uid + ".jpg"));
ResponseText("<?xml version=\"1.0\" ?><root><face success=\"1\"/></root>");
return;
}
}
public bool IsReusable
{
get
{
return false;
}
}
private void CreateDir(string uid)
{
string avatarDir = string.Format("../image/upload/idcardphoto/{0}",
uid);
if (!Directory.Exists(GetMapPath(avatarDir)))
Directory.CreateDirectory(GetMapPath(avatarDir));
}
private void ResponseText(string text)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(text);
HttpContext.Current.Response.End();
}
private string UploadTempAvatar(string uid)
{
string filename = uid + ".jpg";
string uploadUrl = GetRootUrl("../image/") + "upload/idcardphoto";
string uploadDir = GetMapPath("..\\image\\upload\\idcardphoto");
if (!Directory.Exists(uploadDir + "temp\\"))
Directory.CreateDirectory(uploadDir + "temp\\");
filename = "temp/" + filename;
if (HttpContext.Current.Request.Files.Count > 0)
{
HttpContext.Current.Request.Files[0].SaveAs(uploadDir + filename);
}
return uploadUrl + filename;
}
private byte[] FlashDataDecode(string s)
{
byte[] r = new byte[s.Length / 2];
int l = s.Length;
for (int i = 0; i < l; i = i + 2)
{
int k1 = ((int)s[i]) - 48;
k1 -= k1 > 9 ? 7 : 0;
int k2 = ((int)s[i + 1]) - 48;
k2 -= k2 > 9 ? 7 : 0;
r[i / 2] = (byte)(k1 << 4 | k2);
}
return r;
}
private bool SaveAvatar(string avatar, string uid)
{
byte[] b = FlashDataDecode(HttpContext.Current.Request[avatar]);
if (b.Length == 0)
return false;
string size = "";
if (avatar == "avatar1")
size = "large";
else if (avatar == "avatar2")
size = "medium";
else
size = "small";
string avatarFileName = string.Format("../image/upload/idcardphoto/{0}/{1}.jpg",
uid, size);
FileStream fs = new FileStream(GetMapPath(avatarFileName), FileMode.Create);
fs.Write(b, 0, b.Length);
fs.Close();
return true;
}
public static string GetRootUrl(string forumPath)
{
string ApplicationPath = HttpContext.Current.Request.ApplicationPath != "/" ? HttpContext.Current.Request.ApplicationPath : string.Empty;
int port = HttpContext.Current.Request.Url.Port;
return string.Format("{0}://{1}{2}{3}/{4}",
HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.Url.Host,
(port == 80 || port == 0) ? "" : ":" + port,
ApplicationPath,
forumPath);
}
public static string GetMapPath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\"))
{
strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}
}