当前位置: 移动技术网 > IT编程>开发语言>Jquery > ajaxfileupload 文件上传

ajaxfileupload 文件上传

2018年06月07日  | 移动技术网IT编程  | 我要评论

 先引用 ajaxfileupload.js   没有自己去csdn下

 

接下来是HTML


 

<input type="file" id="file1" name="file" style="display: none" accept="image/x-png,image/gif,image/jpeg,image/bmp" multiple="multiple" />
  <button type="button" class="btn btn-info" id="btnFile"><i class="fa fa-cloud-upload">&nbsp;上传</i></button>

其中 accept  可以指定上传格式  multiple="multiple"  此属性可进行多文件选择

 

JS部分

 

 $("#btnFile").click(function () {
        $("#file1").click();
    })
    $("#file1").change(function () {
        if ($("#file1").val().length > 0) {
            ajaxFileUpload();
        }
        else {
            swal({ title: '请选择上传文件!', text: '', type: 'info' });
        }
    });

 

 function ajaxFileUpload() {
        $.ajaxFileUpload
        (
            {
                url: '/FinancialReimbursement/FinancialReimbursement/Upload', //用于文件上传的服务器端请求地址
                type: 'post',
                data: { Id: '123', name: 'add' }, //此参数非常严谨,写错一个引号都不行
                secureuri: false, //是否需要安全协议,一般设置为false
                fileElementId: 'file1', //文件上传域的ID
                dataType: 'json', //返回值类型 一般设置为json
                success: function (data, status)  //服务器成功响应处理函数
                {
                    alert("上传成功");
                },
                error: function (data, status, e)//服务器响应失败处理函数
                {
                    alert(e);
                }
            }
        )

 

后台部分 

 public ActionResult Upload()
        {
            NameValueCollection nvc = System.Web.HttpContext.Current.Request.Form;

            string type = nvc.Get("name");//获取参数

           HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

          

  if (hfc.Count > 0 )
            {
                #region  执行多个文件上传
                for (int i = 0; i < hfc.Count; i++)
                {
                    var FileNameArr = hfc[i].FileName.Split('.');
                   
                    string FileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "_" + FileNameArr[0] + "." + FileNameArr[1];
                    imgPath += "/AllFileUp/ForTheAttachment/" + FileName + ",";
                    string imgP = "/AllFileUp/ForTheAttachment/" + FileName;
                    string PhysicalPath = Server.MapPath(imgP);
                   if (!Directory.Exists(Server.MapPath("/AllFileUp/ForTheAttachment")))//存放路径文件夹不存在就自动新建
                   {
                       Directory.CreateDirectory(Server.MapPath("/AllFileUp/ForTheAttachment"));
                   }
                    hfc[i].SaveAs(PhysicalPath);                 
                }
                #endregion 
            }

  return Json(new { count = hfc.Count});//返回保存文件的个数

      } 

 

   写的很乱 第一次写 一点格式都没有 哈哈哈 

 

 

 

 

  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网