资源下载地址:http://download.csdn.net/detail/peiyuanxin/9555636
引用的JS 加CSS
<link type="text/css" rel="stylesheet" href="~/Content/webuploader.css" />
<link type="text/css" rel="stylesheet" href="~/Content/diyUpload.css" />
<script type="text/javascript" src="~/Scripts/jquery-1.8.3.js"></script>
<script type="text/javascript" src="~/Scripts/diyUpload.js"></script>
<script type="text/javascript" src="~/Scripts/webuploader.html5only.min.js"></script>
<style>
*{ margin:0; padding:0;}
#box{ margin:50px auto; width:540px; min-height:400px; background:#FF9}
#demo{ margin:50px auto; width:540px; min-height:800px; background:#CF9}
</style>
<body>
<input type="file" name="file" class="webuploader-element-invisible" multiple="multiple" accept="image/*">
<div id="box">
<div id="test"></div>
</div>
</body>
<script type="text/javascript">
$('#test').diyUpload({
url:'/File/Upload', //控制器名称
success:function( data ) { //成功返回的数据
console.info(data);
},
error:function( err ) { //失败返回的数据
console.info( err );
},
buttonText : '选择文件',
chunked:true,
// 分片大小
chunkSize:512 * 1024,
//最大上传的文件数量, 总文件大小,单个文件大小(单位字节);
fileNumLimit:50,
fileSizeLimit:500000 * 1024,
fileSingleSizeLimit:50000 * 1024,
accept: {}
});
</script>
控制器中的代码:
public ActionResult Upload(HttpPostedFileBase file)
{
if (file == null)
{
return Content("没有文件!");
}
//Random r = new Random(100); //产生一个随机数据
string Extends = DateTime.Now.ToFileTime().ToString(); //转换成windows文件夹时间
//获取文件的后缀名称
string geshi = file.FileName.Substring(file.FileName.IndexOf('.'));
//保存的路径
string path = Path.Combine(Request.MapPath("~/Images"), Extends+geshi);
try {
file.SaveAs(path);
}catch(Exception x)
{
return Content("上传失败!");
}
return Content("上传成功!");
}