1、jquery.form附件上传

1、将jquery及jquery.form引用进来

    <script src="~/Content/JQuery/jquery-1.8.2.js"></script>
    <script src="~/Content/JQuery/jquery.form.js"></script>

2、html页面代码

    <form id="ajaxForms" enctype="multipart/form-data">
        <input id="Files" name="Files" type="file" class="file" />
        <input id="Button1" οnclick="Fn_UpLoad()" type="button" value="上传附件" />
    </form>
    <table id="List"></table>

3、js前台代码

        function Fn_UpLoad() {
            if (document.getElementById("Files").value) {
                $("#ajaxForms").ajaxSubmit({
                    type: "POST",
                    url: "/Form/Fn_UpLoad?SaveModuleName=TestFile",
                    dataType: "text",
                    success: function (data) {
                        $("#List").append("<tr><td>" + data + "</td></tr>");
                    }
                });
            } else {
                alert("请选择附件!");
                return;
            }
        }

4、MVC后台代码

        public ActionResult Fn_UpLoad()
        {
            HttpPostedFileBase files = Request.Files["Files"];
            string FileName = files.FileName; 

            string SaveModuleName = Request["SaveModuleName"].ToString();//文件存放模块
            string FilesPath = System.AppDomain.CurrentDomain.BaseDirectory + "Attachment\\" + SaveModuleName;//文件存放路径

            //判断文件存放模块是否存在
            if (Directory.Exists(FilesPath) == false)
            {
                Directory.CreateDirectory(FilesPath);
            }

            //判断文件是否
            if (System.IO.File.Exists(FilesPath + "\\" + FileName) == false)
            {
                files.SaveAs(FilesPath + "\\" + FileName);
            }
            return Content(FilesPath + "\\" + FileName);
        }


5、最终效果


6、源代码

本例子源代码



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值