【发布时间】: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 调用可能很有用。