WCF, CORS, Base64String ( , ). → fooobar.com/questions/117389/... img , , jquery.
, ( , , Base64Strings), WCF, ( , WCF jquery).
<?xml version="1.0"?>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="EmployeeService.EmployeeSearchService/*Your service name replaces EmployeeService.EmployeeSearchService */" behaviorConfiguration="DefaultServiceBehavior">
<endpoint binding="webHttpBinding" contract="EmployeeService.IEmployeeSearchService" behaviorConfiguration="DefaultEPBehavior" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="DefaultEPBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="DefaultServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
<connectionStrings>
<add name="myConnString" connectionString=""/>
</connectionStrings>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Script.Serialization;
namespace EmployeeService
{
public class EmployeeSearchService : IEmployeeSearchService
{
public List<Customer> GetAllImages()
{
WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Access-Control-Allow-Origin", "*"); WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Access-Control-Allow-Methods", "POST"); WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Access-Control-Allow-Headers", "Content-Type, Accept");
List<Base64String> s = new List<Base64String>();
var jsonData = new
{
totalImages = s.Count,
Images = (
from row in s
select new
{
imgString = row
}
).ToArray()
};
return (new JavaScriptSerializer()).Serialize(jsonData);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using EmployeeSearchService.BE;
namespace EmployeeService
{
[ServiceContract]
public interface IEmployeeSearchService
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
object GetAllImages();
}
}
jquery , , count Image, "result.Image [0]"
$.support.cors = true;
var methodName = '/GetAllImages';
var urlService = 'some http service url';
$.ajax({
type: "GET",
url: urlService + methodName,
data: {},
dataType: "Json",
processdata: "false",
success: function (result) {
if (result != null) {
images = $.parseJSON(result);
for(var i = 0 ; i < result.totalImages ; i++)
$('<img>').src('data:image/png;base64,' + result.Image[i]).insertAfter('#myDiv');
}
else { }
},
error: function (msg) {
alert(msg);
}
});
, .