Friday, December 26, 2008

Back to Basics: .NET 2.0 Document Streaming via WSI 1.1 Basic Profile Web Service

This post is for a colleague who will be working on streaming documents through a web service in .NET Framework 2.0. The interface being developed is mandated not use WCF, SOAP attachments or web service extensions.

The below sample demonstrates the use of a base64BinaryByteArray to recompose a physical document from a stream of bytes.


using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Text;
using System.Drawing;

[WebService(Namespace = "http://simeonlobo.blogspot.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public void CreateRecordWithDocument(string filename,
byte[] base64BinaryByteArray,
string documentID)
{
try
{
//Example only: Recompose the document from a stream on NTFS
FileStream fs = new(FileStream filename, FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter writer = new BinaryWriter(fs);
fs.Write(base64BinaryByteArray, 0, base64BinaryByteArray.Length);
fs.Close();

//Example only: Delete temporary document after you are finished with it
File.Delete("D:\\" + filename);
}
catch (Exception exc)
{
WriteToErrorLog("CreateRecordWithDocument", exc.Message.ToString(), "D:\\error.log");
}
}

public void WriteToErrorLog(string errorNumber, string errorDescription, string logFilePhyPath)
{
// create a writer and open the file
TextWriter tw = new StreamWriter(logFilePhyPath);

// write a line of text to the file
tw.WriteLine(DateTime.Now + ", " + errorNumber + ", " + errorDescription);

// close the stream
tw.Close();
}

}

0 comments:

  Personal Web Log of Simeon Lobo, 2001 - 2009

Back to TOP