How to Read Binary Documents (in base64 binary format) by ASP.NET Web Service

Add new web service in your ASP.NET web application.

Adding a web service will create two file in your web application:

1)      ASMX FILE(For example WebService.asmx)

2)      CS FILE(in App_Code folder, like WebService.cs)

Create web method inside it as follow.  It takes document name as parameter and returns array of byte (binary data).

 

[WebMethod]

public Byte[] ReadDocument(string strDocumentName)

{

string strdocPath;

strdocPath = “D:\\TESTDIR1\\TESTDIR2″ + strDocumentName;

FileStream objfilestream = new FileStream(strdocPath,FileMode.Open,FileAccess.Read);

int docLen = (int)objfilestream.Length;

Byte[] arrDocumentContents  = new Byte[docLen];

objfilestream.Read(arrDocumentContents,0,len);

objfilestream.Close();

return arrDocumentContents;

}

 

Above example reads file from a physical path on a system. You can also use Server.MapPath() for specifying virtual directory.

You can call webservice by using following like URL

http://yourdomain / WebService.asmx/ ReadDocument? strDocumentName=test.gif

 

Web service will return base64 binary data as follow:

<?xml version=”1.0″ encoding=”utf-8″?>

<base64Binary xmlns=”http://tempuri.org/”>R0lGODlhCAAFAIABAMaAgP///yH5BAEAAAEALAAAAAAIAAUAAAIKBBKGebzqoJKtAAA7</base64Binary&gt;


Advertisements

Display Image in html as Binary Data with Base64 encoding

In HTML, traditionally we use path of image file to specify image source in IMG tag.

However, we can give binary data as well as source in “SRC” attribute of IMG tag, and image will be display in html.

Syntax:

<IMG SRC=”data:image/gif;base64,<Binary Data>” />

Example:

<IMG SRC=”data:image/gif;base64, R0lGODlhCAAFAIABAMaAgP///yH5BAEAAAEALAAAAAAIAAUAAAIKBBKGebzqoJKtAAA7″ />

Binary data in above line is ‘base64 encoded’ format of image.

One advantage of this method is that visitor never knows the PATH of image you have displayed.

Drawback of this point is that IE does not support this way to display image.

 

Other Technical Stuff from Siddharth:

Show current date using Javascript in web browser(Client side date in web browser
https://siddharthboraniait.wordpress.com/2013/09/11/show-current-date-using-javascript-in-web-browserclient-side-date-in-web-browser

 

How to make custom Task Scheduler in asp.net c# using thread
https://siddharthboraniait.wordpress.com/2013/09/06/howto-make-custom-task-scheduler-in-asp-net-csharp-using-thread

 

Introduction to Andriod – First step towards Andriod application development
https://siddharthboraniait.wordpress.com/2011/07/18/know-andriod-introduction-to-andriod