转了一篇jquery的上传控件使用博文,但是,经过测试貌似不行,自己研究了一下,效果实现。记下,以后使用。
下载“Uploadify”,官方版本为php的,很多文件不需要,删除带.php的文件。
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>
<link href="js/uploadify/uploadify.css" type="text/css" rel="Stylesheet"/>
<script type="text/javascript" src="js/uploadify/jquery.uploadify.js"></script>
<script type="text/javascript" src="js/uploadify/jquery.uploadify.min.js"></script>
<script type="text/javascript">
$(function () {
$("#uploadify").uploadify({
'buttonClass': 'button',
'height': '20',
'width':'30',
'swf': './js/uploadify/uploadify.swf',
'cancelImg': './js/uploadify/uploadify-cancel.png',
'folder': './upload',
'buttonText': '上传',
'uploader': './UpLoadHandler.ashx',
'queueID': 'd',
'auto': 'true',
'multi': true,
'fileTypeDesc': 'Wrod文档(*.doc,*.docx)|Excel文件(*.xls,*.xlsx)|图片文件(*.jpg;*.png;*.gif;*.bmp;*.jpeg;*.tiff;*.dwg)|压缩文件(*.zip;*.rar;*.7z)',
'fileTypeExts': '*.doc;*.docx|*.xls,*.xlsx|*.jpg;*.png;*.gif;*.bmp;*.jpeg;*.tiff;*.dwg|*.zip;*.rar;*.7z',
'onUploadSuccess': function (file, data, response) {
if (data.indexOf('错误提示') > -1) {
alert(data);
}
else {
alert("上传成功!");
}
},
'onUploadError': function (file, errorCode, errorMsg, errorString) {
alert('文件:' + file.name + ' 上传失败: ' + errorString);
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="d"></div>
<input type="file" id="uploadify" name="uploadify" />
</div>
</form>
</body>
</html>
后台代码:
在一般处理程序里
public class UpLoadHandler : IHttpHandler,System.Web.SessionState.IRequiresSessionState{
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8";
string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
HttpPostedFile file = context.Request.Files["Filedata"];
if (file != null)
{
if (!System.IO.Directory.Exists(uploadPath))
{
System.IO.Directory.CreateDirectory(uploadPath);
}
context.Session["FilePath"] = uploadPath + file.FileName;//将上传文件路径保存到session 用于跨页传递
file.SaveAs(uploadPath + file.FileName);
}
//context.Response.Write("Hello World");
}
public bool IsReusable {
get {
return false;
}
}
}