【问题标题】:Send Image using Ajax Call and transfer code behind web method asp.net c#使用Ajax调用发送图像并在web方法asp.net c#后面传输代码
【发布时间】:2016-10-01 13:37:01
【问题描述】:

我正在尝试使用 Ajax 调用发送图像并在 web 方法 asp.net c# 后面传输代码。

下面是我的脚本。

<script type="text/javascript">
    function SubmitFunction() {
        alert(imgData_Based64String);
        imgData_Based64String = "test";
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "index.aspx/SaveImage",
            data: "{ 'Based64BinaryString' :'" + imgData_Based64String + "'}",
            dataType: "json",
            success: function (data) {
                alert("success");
            },
            error: function (result) {
                alert("Error");
            }
        });
    }

</script>

下面是我的图片和按钮代码。

<img src="img.jpg" id="img" width="100px" height="100" />
<asp:Button ID="btnCapture" Text="Capture" runat="server" OnClientClick="SubmitFunction();"
        OnClick="btnCapture_Click" />

C# 背后的代码。

[System.Web.Services.WebMethod]
public static void SaveImage(string Based64BinaryString)
{
    string Value = Based64BinaryString;       
}

实际上我是 Ajax 调用的新手,图像应该转换为二进制格式并转换为 Web 方法字符串值。图像 ID 也没有声明到上述脚本代码中以转换二进制格式。我正在尝试一个图像,并将该图像传输到 SQL 表。

【问题讨论】:

  • 目前还不清楚到底是什么问题。究竟是什么不起作用?
  • 如果您尝试发布图片,则内容类型不能为 json。
  • 我想将图像传输到 Web 方法后面的代码以存储到 sql 表中。 @Dekel
  • 那么如何使用 ajax 调用 web 方法传输图像。 @derloopkat
  • 检查这个stackoverflow.com/questions/14575787/… 你可能正在使用一个网络服务,这个代码是用于 MVC 但 jQuery 调用可能很有用。

标签: c# jquery asp.net ajax


【解决方案1】:
function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            $('#falseinput').attr('src', e.target.result);
            var data = {};
            data.image = e.target.result;
            $.ajax({
                type: "POST",
                url: "someWebForm.aspx/SomeWebMethod",
                data: JSON.stringify(data),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    //do something
                }
            });
            return false;
        };
        reader.readAsDataURL(input.files[0]);
    }
}

 [System.Web.Services.WebMethod]
        public static string SomeWebMethod(string image)
        {
            MemberImageData = image;

            return MemberImageData;
        }

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 1970-01-01
    • 2020-01-24
    • 2015-07-31
    • 2013-05-21
    • 2012-06-22
    • 2021-12-19
    • 2018-06-07
    • 1970-01-01
    相关资源
    最近更新 更多