zoukankan      html  css  js  c++  java
  • ajax提交文件

    $("#file").off("change").on("change",function(){
    var inputFile = $('#file').get(0).files[0];
    if(inputFile && (inputFile.name.endsWith("doc") || inputFile.name.endsWith("docx"))){
    $("#span").html(inputFile.name);
    }else{
    window.wxc.xcConfirm("请上传word文件!", "error");
    return ;
    }
    });

    <form id="addForm" class="form-horizontal" enctype="multipart/form-data">
    <input type="file" accept="application/pdf" id="file" name="file" style="display:none">
    </form>
    $("#BA_Btn").click(function(){
    var s=$("#span").html();
    var form = new FormData(document.getElementById("addForm"));
    $.ajax({
    type: "POST",
    url: "<%=basePath%>backend/sjxq/add",
    data: form,
    async: false,
    processData: false,
    contentType: false,
    success: function (result) {
    window.history.back();
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
    window.wxc.xcConfirm("保存失败", "error");
    return ;
    },
    });
    });

    注意     :上传文件的文件流是无法被序列化并传递的。

    对于单个文件,不放在form表单里面的,比如

    <input type="file" multiple="multiple" name="file" id="file">

    多文件需要遍历append进去,

    function aaa(){
    var formData = new FormData();
    var file = $("#file").get(0).files;
    for(var i=0;i<file.length;i++){
    formData.append('file',file[i]);
    }
    $.ajax({
    url : "http://localhost:8080/datahalls/page/test",
    type : 'POST',
    data : formData,
    processData : false,
    contentType : false,
    cache: false,
    success:function(data){
    alert(111)
    }
    })

    }

  • 相关阅读:
    转Vtype扩展
    Can't connect to MySQL server on 'ip' (13)
    观察者+js 模式
    (转)ASP.NET架构分析
    sql得到时间
    Js+XML 操作 (转)
    js中的math对象
    property和attribute的区别
    CSS样式定义
    linux 开启 mount
  • 原文地址:https://www.cnblogs.com/foreverstudy/p/12744281.html
Copyright © 2011-2022 走看看