收藏
回答

如何通过wx.request获取webservice连接的SQLserver中Image图片并显示?

1.我在SQLserver中存储有Image格式的图片。

2.建了IIS,通过webservice可以从SQLserver中获取包含Image的记录,可以通过访问网址看到。

3.小程序中通过request调用这个webservice时,可以获取Image的二进制数据流,但是无法显示出来。

请大神指点,多谢!!

这是webservice的程序:

 [WebMethod]

        public string GetImageJHDH()             //从服务器获取图片

        {

            string list;

            DataSet ds = new DataSet();

            DataTable dt = new DataTable();

            SqlConnection conn = new SqlConnection("Data Source= ;Initial Catalog= ;Persist Security Info=True;User ID= ;Password= ");

            conn.Open();

            string sql = "select * from T_Image where D_JHDH like 'XPD2104'  order by ID desc";

            SqlDataAdapter cmd = new SqlDataAdapter(sql, conn);

            cmd.Fill(ds);

            dt = ds.Tables[0];

            conn.Close();

            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();

            jsSerializer.MaxJsonLength = Int32.MaxValue;

            List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();

            Dictionary<string, object> childRow;

            foreach (DataRow row in dt.Rows)

            {

                childRow = new Dictionary<string, object>();

                foreach (DataColumn col in dt.Columns)

                {

                    childRow.Add(col.ColumnName, row[col]);

                }

                parentRow.Add(childRow);

            }

            list = jsSerializer.Serialize(parentRow);

            return list;

        }

这是开发者工具中的程序JS:

requestWebServiceGetImage:function(){             //查询图片信息

    var that = this

    wx.request({

      //请求的地址路径

      url:'https://www. .xyz/WebService.asmx/GetImageJHDH',

      datatype:"json",

      //请求方式,默认为get

      method:"POST",

      //请求的标题,不能省略

      header:{

        'content-type':'application/octet-stream'

      },

      success:function(res){

        console.log(res);

        var realobj = res.data;

        console.log(realobj);

        that.setData({

            ima:realobj,

        })

      },

      fail:function(err){

        console.log(err)

      }

    })

  },

wxml程序:

 <block wx:for="{{ima}}wx:key="ID">

       <image class= "imagesrc='{{item.Image}}mode="aspectFitbindtap='previewImagedata-src="{{ima.Image}}"></image>

  </block>>



回答关注问题邀请回答
收藏
登录 后发表内容
问题标签