C# 如何将文件与其他数据一起发送到webmethod?不使用任何插件

C# 如何将文件与其他数据一起发送到webmethod?不使用任何插件,c#,javascript,jquery,asp.net,C#,Javascript,Jquery,Asp.net,好的,我有两个输入字段,我将这些字段的数据发送到服务器端webmethod。这很好用。但我也希望将文件与其他输入字段的数据一起发送到相同的webmethod(而不是ashx hander)。你能帮帮我吗 这将调用在数据库中存储数据的webmethod function SendToServer(name,lineitems) { $.ajax({ type: "POST", url: "test.aspx/P

好的,我有两个输入字段,我将这些字段的数据发送到服务器端webmethod。这很好用。但我也希望将文件与其他输入字段的数据一起发送到相同的webmethod(而不是ashx hander)。你能帮帮我吗

这将调用在数据库中存储数据的webmethod

function SendToServer(name,lineitems) {
            $.ajax({
                type: "POST",
                url: "test.aspx/PostDataWM",
                data: JSON.stringify({ name: name,lineitems: lineitems }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                success: function (data, status) {
//                    console.log("CallWM");
                    alert(data.d);
                },
                failure: function (data) {
                    alert(data.d);
                },
                error: function (data) {
                    alert(data.d);
                }
            });
        }
文件上载标记

  <span style="font-family: Arial">Click to add files</span>&nbsp;&nbsp;
        <input id="Button1" type="button" value="add" onclick="AddFileUpload()" />
        <br />
<br />
        <div id="FileUploadContainer">
            <!--FileUpload Controls will be added here -->
        </div>
     <script type="text/javascript">
         var counter = 0;
         function AddFileUpload() {
             var div = document.createElement('DIV');
             div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + '" type="file" class="clsFileUpload" /><input id="Button' + counter + '" type="button" value="Remove" onclick = "RemoveFileUpload(this)" />';
             document.getElementById("FileUploadContainer").appendChild(div);
             counter++;
         }
         function RemoveFileUpload(div) {
             document.getElementById("FileUploadContainer").removeChild(div.parentNode);
         }
     </script>

不需要对数据进行字符串化
直接试试。您可以在代码级别获取数据

            type: "POST",
            url: "test.aspx/PostDataWM",
            data: { name: name,lineitems: lineitems },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,

将名称和行项目隐藏输入添加到文件上载form@dandavis让我试试that@dandavis成功了。我以前没想过。谢谢。但是出于好奇,如果我想在webmethod中这样做,有可能吗?是的,您可以使用FormData()将文件附加到ajax post,或者您可以编写自定义处理程序并将base64文件数据作为字符串变量发布。不是处理程序,我希望在同一webmethod中获得数据和文件。我认为我不能在WebMethod中使用HttpContext。
            type: "POST",
            url: "test.aspx/PostDataWM",
            data: { name: name,lineitems: lineitems },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,