none
开启ajax的 wcf 可以返回 byte[] 吗? RRS feed

  • 问题

  • public byte[] GetMyBytes() { MemoryStream S = new MemoryStream(); //...... return S.ToArray(); }

    在已经开启 AJAX 的 WCF 中,像这样的函数,怎么才能在 js 中调用?

    2018年1月31日 10:30

答案

  • 图像也以用base64字符串表达。

    using (Image image = Image.FromFile(Path))
    {
        using (MemoryStream m = new MemoryStream())
        {
            image.Save(m, image.RawFormat);
            byte[] imageBytes = m.ToArray();
    
            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }
    }


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    • 已标记为答案 fss199 2018年2月2日 8:49
    2018年2月2日 0:49

全部回复

  • https://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery

    Calling WCF Services using jQuery

    $.ajax({ type: Type, //GET or POST or PUT or DELETE verb url: Url, // Location of the service data: Data, //Data sent to server contentType: ContentType, // content type sent to server dataType: DataType, //Expected data format from server processdata: ProcessData, //True or False success: function(msg) {//On Successfull service call ServiceSucceeded(msg); }, error: ServiceFailed// When Service call fails });

    其次,为什么要返回字节数组,我认为返回字符串更合适,可能是因为是图象,但是可以返回bas64字符串,它也是一个图片

     #LoginPanel .logo {
                margin: 0 auto 20px auto;
                width: 128px;
                height: 128px;
                background: transparent url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOvwAADr8BOAVTJAAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTFH80I3AAAREklEQVR4Xu1diXabuhbtp/Fp/M57H3Bv0ySlvYmboXYSO/GEZwNm8Jypd1x+2jzhS7BsBAEPVHutvTqYwdY+OjoSR4cPAgICAgICAgI/C25u7mXTNOWXlxfZshzFMMzFbDZbPD8/L56enpZ8fHxc/kmOW2jagNBUJpOJrKpN+fr6TqKXFNhXF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exSdQzPl7i++D8k+NsBLV7B2yWJF5ho/vFZ+iBdFnV3e1LgjQy5Rq6RE/G/4FELNezRBXaT5yLolgkiBVj/LZQLlexvRx1fLndcpKE0aAMK4nohei7BraNDQambNsjBUmTSRsEPAoEt0n8gaFm1xXDBEJwQmIA1B0g7l8m7llGWRq4euTdYWiAoEHCjeNz7C1EoNlu626wqWmGfHZWEIILCAgICAgIZBwfPvwPYzJ1DskIsl4AAAAASUVORK5CYII=) no-repeat center center;
            }

           overlay: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAQMAAABIeJ9nAAAAA3NCSVQICAjb4U" +
                        "/gAAAABlBMVEX///8AAABVwtN+AAAAAnRSTlMA/1uRIrUAAAAJcEhZcwAAAsQAAALEAVuRnQsAAAAWdEVYdENyZWF0" +
                        "aW9uIFRpbWUAMDQvMTMvMTGrW0T6AAAAHHRFWHRTb2Z0d2FyZQBBZG9iZSBGaXJld29ya3MgQ1M1cbXjNgAAAAxJREFUCJljaGBgAAABhACBrONIPgAAAABJRU5ErkJggg==",


    2018年2月1日 0:54
  • 谢谢您在百忙之中予以指导!您那个实例只有传输字符串的,有没有传输图像的实例?
    2018年2月1日 12:56
  • 图像也以用base64字符串表达。

    using (Image image = Image.FromFile(Path))
    {
        using (MemoryStream m = new MemoryStream())
        {
            image.Save(m, image.RawFormat);
            byte[] imageBytes = m.ToArray();
    
            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }
    }


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    • 已标记为答案 fss199 2018年2月2日 8:49
    2018年2月2日 0:49