0

I am trying to get a base64 string of a jpg image from a C# ASP method. I have confirmed that the base64 being returned is actually converted into the image correctly. However, when trying to set it as an 's src through JQuery, I get a BAD REQUEST error in FireBug monitor.

JQuery:

function GetImage() {
    $.ajax({
        url: "Default.aspx/GetImageArray",
        type: "POST",
        dataType: "json",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        success: function (msg) {
            document.getElementById("MainContent_output").setAttribute('src',     'data:image/jpeg;base64,' + $.fromBASE64(msg['d']));
        },
        error: function (e) {
            alert(JSON.stringify(e));
        }
    });
}

C# ASP:

[WebMethod]
public static string GetImageArray()
{
    return Convert.ToBase64String(File.ReadAllBytes(path));
}

Don't mind the 'GetImageArray' name, the method returns a string.

Well, SOS pretty much :)

4

1 回答 1

0

对于那些需要答案的人,我通过以下方式解决了它:

C# (ASP) (不变):

[WebMethod]
public static string GetImageArray()
{
    return Convert.ToBase64String(File.ReadAllBytes(path.jpg));
}

查询:

function GetImage() {
    $.ajax({
        url: "Default.aspx/GetImageArray",
        type: "POST",
        dataType: "json",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        success: function (msg) {
            document.getElementById("MainContent_output").setAttribute('src', 'data:image/jpg;base64,' + msg['d']);
        },
        error: function (e) {
            alert(JSON.stringify(e));
        }
    });
}

注意:asp:Image 控件中的元素“输出”。剩下的唯一问题是字符串对于 Javascript Serializer 来说太长了,这发生在大图像上。当我弄清楚时,我会回复一个修复。

于 2013-11-05T19:50:17.097 回答