将System.Byte []转换为字符串或图像格式

时间:2017-06-08 08:04:15

标签: javascript c#

我们有C#MVC模型网络应用程序 从oracle接收的blob数据和代码之后的集成是

    foreach (DataRow oRow in oDataTable.Rows)
    {
    Rel oUnitData = new Rel();

    if (!string.IsNullOrEmpty(oRow["LOG_SNAP"].ToString()))
    oUnitData.imgdisp = oRow["LOG_SNAP"].ToString();

     lUnitData.Add(oUnitData);
    }

    and for display using javascript file in that datatables 
    following part of code

    { data: 'imgdisp', "render": function (data) {

    var str = data
    var base64string = Convert.ToBase64String(str);
    return '<img src="data:image/jpeg;base64,+ base64_decode(str)+">';
    }

    Java Script Code 
------------------------------------------------

function LoadImageDispData() {

       var jsontext = JSON.stringify({
       });

       $.ajax({
           url: '/Rel_ProjectStatusUpdate.aspx/LoadImageDisp',
           data: jsontext,
           type: 'POST',
           async: true,
           contentType: 'application/json; charset=utf-8',
           dataType: 'json',
           success: function (result) {

               if (result.d != null) {

                   oTableData = $("#tbl_imagedisp").dataTable().fnDestroy();

                   oTableData = $("#tbl_imagedisp").DataTable({
                       data: result.d,
                       responsive: true,
                       "bAutoWidth": true,
                       "bPaginate": true,
                       "bInfo": true,
                       "bFilter": true,

                       "aoColumns": [



                     { data: 'imgdisp', "render": function (data) {

                         var str = data

                         var base64string = Convert.ToBase64String(str);

                         alert(base64string);

                         return '<img src="data:image/jpeg;base64,+ base64_encode($str)+">';


                     }
                     }


                 ],

                       "columnDefs": [
                    { "visible": false, "targets": [] },
                { className: "dt-left", "targets": [] },
                { className: "dt-center", "targets": [] },
              { className: "dt-right", "targets": [] }
        ],

                       //"order": [[1, 'asc']],


                       dom: 'Bfrtip',
                       buttons: ['copy', 'excel'],

                       "aLengthMenu": [[25, 50, 75, -1], [25, 50, 75, "All"]],
                       "iDisplayLength": 25,
                       "bAutoWidth": true,
                       "language": {
                           //                  
                           //                    "zeroRecords": "Nothing found - sorry",
                           //                    "infoEmpty": "No records available"
                           "emptyTable": "No data available in table"

                       }


                   });

               }

               else {


               }

           }
       });

   }

Data Base Layer Code 
----------------------------------------
public DataTable GetImageDisp()
        {
            DataTable oDataTable = new DataTable();
            try
            {
                LoggingManager.LogDetails("WebSiteModel-Begin GetImageDisp");
                List<OracleParameter> lParameterList = new List<OracleParameter>();
                lParameterList.Add(new OracleParameter("O_IMGDATA", OracleDbType.RefCursor, ParameterDirection.Output));
                oDataTable = this.GetDataTableREL("REL_GET_IMAGEDISP", lParameterList);


                if (oDataTable != null)
                    LoggingManager.LogDetails("WebSiteModel-End GetImageDisp", string.Format("GetImageDisp Data Count:{0}", oDataTable.Rows.Count));
                else
                    LoggingManager.LogDetails("WebSiteModel-End GetImageDisp", string.Format("GetImageDisp Data Count:null"));
            }
            catch (Exception oException)
            {
                LoggingManager.LogDetails("WebSiteModel-Exception GetImageDisp", oException);
                ExceptionManager.LogSiteException(oException, "DrishtiCane.Model.WebSite.Implementation.WebSiteModel::GetImageDisp");
            }

            return oDataTable;
        }


Business Layer
--------------------------------

public List<Rel> GetImageDisp()
        {
            DataTable oDataTable = null;
            List<Rel> lUnitData = new List<Rel>();
            try
            {
                LoggingManager.LogDetails("WebSiteIntegration-Begin GetImageDisp");
                oIWebSiteModel = this.ModelFactory.CreateObject<IWebSiteModel>();
                oDataTable = oIWebSiteModel.GetImageDisp();
                if (oDataTable != null)
                {
                    if (oDataTable.Rows != null && oDataTable.Rows.Count > 0)
                    {
                        foreach (DataRow oRow in oDataTable.Rows)
                        {
                            Rel oUnitData = new Rel();

                            if (!string.IsNullOrEmpty(oRow["LOG_SNAP"].ToString()))
                                oUnitData.imgdisp = oRow["LOG_SNAP"].ToString();


                            lUnitData.Add(oUnitData);
                        }
                    }
                    else
                        lUnitData = null;
                }
                else
                    lUnitData = null;

                if (lUnitData != null)
                    LoggingManager.LogDetails("WebSiteIntegration-End GetImageDisp", string.Format("GetImageDisp Data Count:{0}", lUnitData.Count));
                else
                    LoggingManager.LogDetails("WebSiteIntegration-End GetImageDisp", string.Format("GetImageDisp Data Count:null"));

                // LoggingManager.LogDetails("End GetSubCMasterData", string.Format("Legend List Count:{0}", lVillageMasterList.Count));
            }
            catch (Exception oException)
            {
                LoggingManager.LogDetails("WebSiteIntegration-Exception GetImageDisp", oException);
                ExceptionManager.LogSiteException(oException, "DrishtiCane.Integration.WebSite.Implementation.WebSiteIntegration::GetImageDisp");
            }

            return lUnitData;
        }


Code Behind .aspx file
-----------------------------------------
[WebMethod(EnableSession = true)]
        public static List<Rel> LoadImageDisp()
        {
            List<Rel> lDatalist = new List<Rel>();
            try
            {
                IControlViewController oIControlViewController = oContollerFactory.CreateObject<IControlViewController>();
                lDatalist = oIControlViewController.GetImageDisp();
            }

            catch (Exception oException)
            {
                ExceptionManager.LogSiteException(oException, "DrishtiCane.SiteView.Report:LoadImageDisp");
            }

            return lDatalist;

        }

我们有来自oracle的C#MVC模型web应用程序blob数据,并且代码集成了

1 个答案:

答案 0 :(得分:0)

公平地说,我无法理解你的代码。而且我不确定为什么要生成img标记,而不是创建可以返回实际图像的端点。 但是,你的img标签有一个问题是它需要一个以base 64格式编码的字符串,而且我不确定你的&#34; str&#34;是。

基本上,要在C#中创建base64字符串,可以使用以下代码:

var byteArray = Encoding.UTF8.GetBytes(data); 
var base64String = Convert.ToBase64String(byteArray);

您可能需要在问题中包含更多信息才能获得更好的回复。