【问题标题】:How do I send texbox and attachement to webmethod using jquery ajax call?如何使用 jquery ajax 调用将文本框和附件发送到 webmethod?
【发布时间】:2015-05-14 06:39:59
【问题描述】:

我正在尝试将文本框和输入文件数据发送到我的网络方法。我已经在谷歌上搜索了很长一段时间,但仍然不确定如何实现这一目标:

Jquery/AJAX 调用:

 var dataToSend = new FormData();           
 dataToSend.append('file',  document.getElementById("myFile").value);
 dataToSend.append('text',  document.getElementById("biddername").value);

            $.ajax({
                type: "POST",
                url: "SupplierMaster.aspx/RegisterSupplier",
                data: dataToSend,
                processData: false,
                contentType: false,
                dataType: false,
                async: true,
                success: function (data, status) {
                    console.log("CallWM");
                    alert(data.d);
                },
                failure: function (data) {
                    alert(data.d);
                },
                error: function (data) {
                    alert(data.d);
                }
            });

        }

网络方法:

 [WebMethod]
    public static string RegisterSupplier(HttpPostedFile file, string biddername)
    {

        return "a";
    }

我好像无法调用 webmethod。

EDIT1(根据 Kashif 的建议):

 $.ajax({
                type: "POST",
                url: "SupplierMaster.aspx/RegisterSupplier",
                data: "{'file' : " + document.getElementById("myFile").value + ",'biddername':" + document.getElementById("txtsuppliername").value + "}",
                async: true,
                contentType: "application/json; charset=utf-8",
                success: function (data, status) {
                    console.log("CallWM");
                    alert(data.d);
                },
                failure: function (data) {
                    alert(data.d);
                },
                error: function (data) {
                    alert(data.d);
                }
            });


[System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod] 
    public static string RegisterSupplier(HttpPostedFile file, string biddername)
    {

        return "a";
    }

*

【问题讨论】:

  • 首先,您尝试在 ajax 中调用的 urlSupplierMaster.aspx/RegisterSupplier,但您没有名称为 RegisterSupplier 的方法,而是向我们展示名称为 SubmitBid 的方法? ?
  • @GuruprasadRao 对不起,我写了 web 方法而不是复制粘贴。我已经更新了问题,但问题仍然存在
  • 你试过放断点吗?它是在使用那个网络方法吗??
  • 是的,我一直在尝试,但不是没有达到那个 webmethod
  • 尝试将datatype 设置为json,例如dataType: "json",

标签: javascript c# jquery asp.net ajax


【解决方案1】:

您不能通过 AJAX 发布文件的内容。您必须使用该文件发布一个表单。您应该搜索如何将带有文件输入的表单发布到 Web 方法。

【讨论】:

    【解决方案2】:

    试试这个。编辑过的。这里我使用 Btoa() 将文件转换为 base64。

    dataToSend: "{'file' : " + btoa(document.getElementById("myFile").value)+ ",'biddername':" +  document.getElementById("biddername").value+ "}",
    
                $.ajax({
                    type: "POST",
                    url: "YourWebMethodClassName.aspx/SubmitBid",
                    data: dataToSend,
                    async: true,
                    contentType: "application/json; charset=utf-8",           
                    success: function (data, status) {
                        console.log("CallWM");
                        alert(data.d);
                    },
                    failure: function (data) {
                        alert(data.d);
                    },
                    error: function (data) {
                        alert(data.d);
                    }
                });
    
            }
    

    C#--编辑代码

    [WebMethod]
        public static string RegisterSupplier(string file, string biddername)
        {
    // Write here logic to convert Base64 to file.
    
            return "a";
        }
    

    【讨论】:

    • [System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] 在c#方法上添加这个
    • 我再次根据您的建议更新了我的问题。它仍然没有击中 webmethod
    • 再次编辑。我没有建议。过程数据:假,内容类型:假,数据类型:假,
    • 这对你有用吗?因为它不在我这里。
    • 你在RegisterSupplier中把HttpPostedFile改成字符串了吗?
    猜你喜欢
    • 2017-06-20
    • 1970-01-01
    • 2015-07-25
    • 2016-11-11
    • 2016-11-11
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多