zoukankan      html  css  js  c++  java
  • ajaxfileupload 文件上传

     先引用 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"  此属性可进行多文件选择    

    <a class="btn btn-success btn-sm"  href="@fjTable.Rows[a]["LJ"]" target="_blank"   title="下载" ><i class="fa fa-cloud-download"></i>&nbsp;查看</a>      target="_blank"   打开新窗体下载    target还有其他几个属性这里就不多说了 

    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});//返回保存文件的个数

          } 

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

      

  • 相关阅读:
    Linux发行版 CentOS6.5 修改默认主机名
    《Linux就该这么学》培训笔记_ch06_存储结构与磁盘划分
    《Linux就该这么学》培训笔记_ch05_用户身份与文件权限
    《Linux就该这么学》培训笔记_ch04_Vim编辑器与Shell命令脚本
    《Linux就该这么学》培训笔记_ch03_管道符、重定向与环境变量
    《Linux就该这么学》培训笔记_ch02_一些必须掌握的Linux命令
    《Linux就该这么学》培训笔记_ch01_部署虚拟环境安装Linux系统
    《Linux就该这么学》培训笔记_ch00_认识Linux系统和红帽认证
    Swift笔记2
    request对象和response对象
  • 原文地址:https://www.cnblogs.com/manwwx129/p/Manwwx129.html
Copyright © 2011-2022 走看看