webform ajax 上传文件+参数

今天维护webform项目时,有个需求需要在一个ajax中上传excel和多个参数。网上没怎么找到答案,这边做个笔记。

首先上页面大体这样

  <form  id= "uploadForm">
        <input type="file" accept=".xlsx,.xls" name="excel" id="file" />
        <input type="button" id="upload" value="Import" />
    </form>

再是jquery代码

<script type="text/javascript">
    $(function () {
        $('#upload').click(function () {
            var formData = new FormData($("#uploadForm")[0]);
            formData.append("name", "zhangsan");
            $.ajax({
                url: 'ImportExcel.aspx',
                type: 'POST',
                data: formData,
                async: false,
                cache: false,
                contentType:false,
                processData: false,
                success: function (returndata) {
                    alert(returndata);
                },
                error: function (returndata) {
                    alert(returndata);
                }
            });
        });
    })
   

后台接收简单的方法

var name = Request["name"];

var file=Request.Files["excel"];

比较简单,但是长时间不用也容易忘记,这里笔记下,也欢迎高手指正。

原文地址:https://www.cnblogs.com/ares-yang/p/7133641.html