前台
引用JS
<script src="/Scripts/validate/valid.custom.tools.js"></script>
<script src="/Scripts/upload/jquery.form.js"></script>
<p class="fbzy">
上传附件:<input type="file" name="hwfile" id="hwfile" class="fjinput" style="width: 280px" />
</p>
<p class="fbzy" style="font-size: 12px; color: #999;">格式限制:doc、docx、xls、xlsx、ppt、pptx、pdf、txt、rar、zip</p>
<p class="fbzy" style="font-size: 12px; color: #999;">大小限制:最大2M</p>
<p class="fbzy"><a href="javascript:;" class="s-orange-btn" id="btnToHw">确 定</a><a href="javascript:;" οnclick="Close(null);" class="s-blue-btn">取 消</a></p>
<script type="text/javascript">
$(function () {
var $hwSummary = $("#hwSummary");
var $hwEndDate = $("#hwEndDate");
var $hwfile = $("#hwfile");
$("#btnToHw").click(function () {
if (!Valid.prototype.Trim($hwSummary.val())) {
AlertMini("a1", "请填写作业说明。", "alert_s.gif", 2);
return;
}
if (Valid.prototype.IsUnSafe($hwSummary.val())) {
AlertMini("a1", "作业说明不支持HTML字符。", "alert_s.gif", 2);
return;
}
if ($hwSummary.val().length > 200) {
AlertMini("a1", "作业说明请控制在200个字符以内。", "alert_s.gif", 2);
return;
}
if (!Valid.prototype.Trim($hwEndDate.val())) {
AlertMini("a1", "请填写截止日期。", "alert_s.gif", 2);
return;
}
if (!Valid.prototype.Trim($hwfile.val())) {
AlertMini("a1", "请先上传附件。", "alert_s.gif", 2);
return;
}
if (Valid.prototype.Trim($hwfile.val())) {
if (!ValidFileSuffix($hwfile.val())) {
AlertMini("a1", "请上传指定的文件格式。", "alert_s.gif", 2);
return;
}
}
var optionInfo = {
url: "/@ViewBag.UserCenterAreasName/course/ToHomework",
type: "POST",
success: function (data) {
if (data == "1") {
AlertMini("a1", "作业发布成功。", "success_s.gif", 2);
var _upTimer = setInterval(function () { window.parent.GetHomeWorkList(1); Close(null); }, 2000);
} else if (data == "2") {
AlertMini("a1", "附件大小超过2M。", "alert_s.gif", 2);
} else {
AlertMini("a1", "作业发布失败,请重试。", "error_s.gif", 2);
}
},
error: function () { AlertMini("a1", "网络繁忙,请稍后再试。", "error_s.gif", 2); }
}
$('#hwForm').ajaxSubmit(optionInfo);
});
})
var validVideoType = "doc|docx|xls|xlsx|ppt|pptx|pdf|txt|rar|zip";
function ValidFileSuffix(val) {
var ext = val.substr(val.lastIndexOf('.') + 1);
if (!Valid.prototype.CheckExt(ext, validVideoType)) { return false; }
return true;
}
</script>
后台
if (Request.Files != null && Request.Files.Count > 0)
{
HttpPostedFileBase inputFile = Request.Files[0];
if (inputFile.ContentLength > 2048 * 1024)//2M
return "2";//文件超出大小
var rspFileModel = Szy.Core.Client.InvokeService.Instance.FileService.UpLoadFile(inputFile);
}
用到公共类里面方法