ajaxfileupload asp.net 的简单使用

本人菜鸟,第一次写博客,不会排版,只是记录工作中常用的东西

ajaxfileupload.js源码:

http://www.rczjp.cn/HTML/110420/20113620053635.html

前台:

<script src="../res/js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="ajaxfileupload.js" type="text/javascript"></script>
<script type="text/javascript">
function ajaxFileUpload() {
$.ajaxFileUpload(
{
url: 'Handler.ashx', //需要链接到服务器地址
secureuri: false,
fileElementId: 'FileUpload', //文件选择框的id属性
dataType: 'json', //服务器返回的格式,可以是json
success: function (data, status) //相当于java中try语句块的用法
{
$("#pic_1").attr("src", "../uploadfiles/" + data.msg + "");
},
error: function (data, status, e) //相当于java中catch语句块的用法
{
alert(data.error);
}
} ); }
</script>
<body>
<table class="pro_pic_tb">
<tbody>
<tr>
<td>
<div class="picdiv3">
<img src="http://www.rhebank.com/admin/images/nopic.jpg" id="pic_1" height="100"
width="100">
<div class="picdel" id="picdel_1">
<img src="http://www.rhebank.com/admin/images/del.gif" onclick="delPic(1,'resume')"></div>
</div>
<p>
<span class="input-file">上传图片
<asp:FileUpload runat="server" ID="FileUpload" onchange="javascript:ajaxFileUpload();"/>
</span>
<input name="photo" class="hidden" id="photo" >
</p>
</td>
</tr>
</tbody>
</table>
</body>

下面是后台程序源码:

 <%@ WebHandler Language="C#" Class="Handler" %>

 using System;
using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain"; if (context.Request.Files.Count > )
{
string path = context.Server.MapPath("~/uploadfiles/"); var file = context.Request.Files[]; string fileName; if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
{
string[] files = file.FileName.Split(new char[] { '\\' });
fileName = files[files.Length - ];
}
else
{
fileName = file.FileName;
} string msg = "";
string strFileName = fileName;
if (string.IsNullOrEmpty(strFileName))
{
msg = "{";
msg += string.Format("error:'{0}',\n", "请选择文件!");
msg += string.Format("msg:'{0}'\n", string.Empty);
msg += "}";
}
else
{
fileName = System.IO.Path.Combine(path, fileName);
file.SaveAs(fileName); msg = "{";
msg += string.Format("error:'{0}',\n", string.Empty);
msg += string.Format("msg:'{0}'\n", strFileName);
msg += "}";
}
context.Response.Write(msg); }
} public bool IsReusable {
get {
return false;
}
} }

没什么特殊的,只是做个标记

上一篇:ASP.NET MVC使用Oauth2.0实现身份验证


下一篇:移动前端开发之viewport的深入理解 --- 待续