如何使用worklight应用程序中的sql / http适配器从现有数据库中检索图像

时间:2014-05-17 06:31:20

标签: ibm-mobilefirst worklight-adapters

我有一个现有的数据库,我必须在我的工作灯应用程序中向用户显示图像列表,以便他们可以选择并添加到购物车。

数据库中的图像列仅包含服务器上的图像路径。 即"记忆/浇头/坚果/ hazelnuts.jpg"     "存储器/浇头/螺母/ macadamia_nuts.jpg"

所以如何获取所有这些图像并在我的工作灯应用程序中显示。

1 个答案:

答案 0 :(得分:0)

从数据库中检索服务器URL和图像路径后,您应该做的是连接服务器URL和图像路径。

让我们在数据库中说我存储了这个:" /uploads/original/6/63935/1570735-master_chief.jpg" ;,所以串联就像这样:

var url = "http://static.comicvine.com" + response.invocationResult.resultSet[0].profileimg;
$("#img1").attr("src", url);


以下是一个工作示例。

单击按钮后,将调用SQL适配器过程并返回存储在数据库中的URL。此网址会插入预先存在的img代码src属性中,然后显示该属性。

您需要采用此实施方案并对其进行更改以满足您的需求。

<强> HTML:

<input type="button" value="insert image" onclick="getImageURL();"/><br>
<img id="img1" src=""/>

<强> JS:

function getImageURL() {
    var invocationData = {
            adapter : 'retrieveImage',
            procedure : 'retrieveImageURL',
            parameters : []
    };

    WL.Client.invokeProcedure(invocationData,{
        onSuccess : retrieveSuccess,
        onFailure : retrieveFailure,
    });
}

function retrieveSuccess(response) {
    var url = "http://static.comicvine.com" + response.invocationResult.resultSet[0].profileimg;
    $("#img1").attr("src", url);
}

function retrieveFailure() {
    alert ("failure");
}

替代JS:
此代码段显示了如何将多个图像添加到动态创建的img标记中。

function retrieveSuccess(response) {
    var url, i;
    for (i = 0; i < response.invocationResult.resultSet.length; i++) {
        url = "http://static.comicvine.com" + response.invocationResult.resultSet[i].profileimg;
        $("#imgholder").append("<li><img src='" + url + "'/></li>");
        // imgholder is a UL in the HTML where the img tags will be appended to.
    };
}

适配器JS:

var procedure1Statement = WL.Server.createSQLStatement("select profileimg from users");
function retrieveImageURL() {
    return WL.Server.invokeSQLStatement({
        preparedStatement : procedure1Statement
    });
}

适配器XML:

<procedure name="retrieveImageURL"/>

在数据库中:

table (users)
|
-- column (profileimg)
------ row contents: some URL pointing to an image, for example: /myimg.png