ajax上传文件

前台

引用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">确&nbsp;&nbsp;定</a><a href="javascript:;" οnclick="Close(null);" class="s-blue-btn">取&nbsp;&nbsp;消</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);

用到公共类里面方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值