zoukankan      html  css  js  c++  java
  • 快速上传文件到文件夹.感觉方便好用.

    1.界面HTML代码.enctype="multipart/form-data"  不可缺. accept:显示上传文件的类型

    <form id="uploadFileForm" method="post" enctype="multipart/form-data">
    <div>
    <input type="hidden" id="uploadFileVersion" name="version" />
    <input type="hidden" id="uploadFileYear" name="year" />
    <input type="file" id="File1" name="fileUpload"  accept=".xls,.doc,.txt,.pdf"  multiple>
    <div><label id="uploadFileName" style="color:red;"></label></div>
    </div>
    </form>

    2.提交真个From表单.其它参数.可以只能采用URL.传参数的方式. async: false,cache: false,contentType: false,processData: false,  必须设置这几个属性.不然存在兼容性问题.

    var formData = new FormData($("#uploadFileForm")[0]);
    var prompt = $.initMask({ content: "正在提交,请稍候..." });
    //点击事件
    $.ajax({
    url: "/PriceSetting/PriceSettingSubmit?company_name=" + company_name + "&pj_name=" + pj_name + "&stage_id=" + stage_id + "&stage_name=" + stage_name + "&desc=" + desc + "&gridjson=" + JSON.stringify(list) + "",
    data: formData,
    type: 'post',
    dataType: 'json',
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    beforeSend: function () {
    //显示等待
    prompt.show();
    },
    success: function (result) {
    alert(result.Message);
    //提示后关闭等待
    prompt.close();
    },
    });

    3.后端代码.

    public ActionResult PriceAdjustmentSubmit()
    {
    //获取URL参数
    var company_name = Request["company_name"];
    var pj_name = Request["pj_name"];
    var stage_id = Request["stage_id"];
    var stage_name = Request["stage_name"];
    var desc = Request["desc"];
    var gridjson = Request["gridjson"];
    bool isreport = true;

    //保存附件到服务器
    //定义文件存放的目标路径
    string proDir = "/Content/PriceSettingFiles";
    string targetDir = System.Web.HttpContext.Current.Server.MapPath("~" + proDir);
    //创建目标路径
    if (!Directory.Exists(targetDir))
    {
    Directory.CreateDirectory(targetDir);
    }
    //获取客户端上传的文件集合
    HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
    string filePath = string.Empty;
    string filename = string.Empty;
    for (int i = 0; i < files.Count; i++)
    {
    HttpPostedFile file = files[i];

    if (file.ContentLength > 1024 * 1024 * 10)
    {
    ret = new
    {
    IsSuccess = false,
    Message = "文件" + file.FileName + "不能超过10M!"
    };
    return Json(ret);
    }
    //取得文件的扩展名,并转换成小写
    string fileExtension = System.IO.Path.GetExtension(file.FileName).ToLower();

    if (!string.IsNullOrEmpty(fileExtension))
    {
    filename = "楼栋价格调整附件" + file.FileName;
    if (!fileExtension.Equals(".docx") && !fileExtension.Equals(".xlsx") && !fileExtension.Equals(".pdf"))//校验文件类型
    {
    ret = new
    {
    IsSuccess = false,
    Message = "上传文件格式不正确"
    };
    return Json(ret);
    }
    }
    //组合成文件的完整路径
    string path = Path.Combine(targetDir, timeStamp + Path.GetFileName(file.FileName));
    filePath = proDir + "/" + timeStamp + file.FileName;
    //保存上传的文件到指定路径中
    if (System.IO.File.Exists(path))
    {
    System.IO.File.Delete(path);
    }
    file.SaveAs(path);//保存部分
    }

    }

        

  • 相关阅读:
    webuploader多次触发注册
    下载二进制文件
    表格打印
    多个请求下 loading 的展示与关闭
    前进刷新后退不刷新
    页面权限控制和登陆验证
    axios.post 配置formdata提交
    react错误边界
    关于beforeRouterEnter获取不到this
    JS
  • 原文地址:https://www.cnblogs.com/TanYong/p/10072617.html
Copyright © 2011-2022 走看看