日期:2011-11-17  浏览次数:20398 次


Developing An Image Upload Web Service

By: Bipin Joshi
Level: Intermediate
Posted Date: 5/31/2002
Tested with ASP.NET v1.0 (RTM)
Click for Printable Version
Click here to download sample code


Member Rating: 3.90 (Rated by 10 members)
Rate This Item

ASP.NET Web Services provide 'Web callable' functions based on industry standards like HTTP, XML and SOAP. Since Web Services heavily rely on XML, all the data that is passed to and returned from a Web Service must be plain text. However, in certain applications we do need to pass binary data. Say for example I want to pass images from my Web form to a Web Service to save in some central repository and then retrieve them back. Does this mean that Web Services cannot be used for such data transfer? Certainly not. In fact ASP.NET Web Services make it easy to pass such data by hiding the encoding and decoding involved. Typically when you want to pass binary data you will declare the parameter of the Web method or return value of the Web method as a byte array. ASP.NET Web Services will automatically encode/decode this data using Base64 encoding. (Base64 encoding is the same encoding that is used for email MIME attachments.) In this example we develop an image upload Web Service that stores and retrieves images from SQL Server database.
SQL Server Database Table
To work with example you need a table in SQL server database called IMAGES. The following script can be used to create this table.
CREATE TABLE [dbo].[IMAGES] (
  [id] [int] IDENTITY (1, 1) NOT NULL ,
  [imgdata] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
The table contains two columns - ID that represents the primary key and IMGDATA that stores binary image data. Note that I have created this table in Northwind database itself; you may want to create it in some other database.
Creating the Web Service
Now, let us proceed with creating the Web Service. Create a new Web Service project in VS.NET and add the following two Web methods to it.
<WebMethod()> Public Function SaveImage(ByVal imgdata() As Byte) As String
  Dim connstr As String = "Integrated Security=SSPI;User ID=sa;Initial Catalog=Northwind;Data Source=SERVER\netsdk"
    Dim cnn As New SqlConnection(connstr)
    cnn.Open()
    Dim cmd As New SqlCommand("insert into images values(@img)", cnn)
    cmd.Parameters.Add(New SqlParameter("@img", imgdata))
    cmd.ExecuteNonQuery()
  End Function
  <WebMethod()> Public Function RetrieveImage(ByVal imgid As Integer) As Byte()
    Dim connstr As String = "Integrated Security=SSPI;User ID=sa;Initial Catalog=Northwind;Data Source=SERVER\netsdk"
    Dim cnn As New SqlConnection(connstr)
    Dim cmd As New SqlCommand("select * from images where id=" & imgid, cnn)
    cnn.Open()
    Dim dr As SqlDataReader = cmd.ExecuteReader
    dr.Read()
    Dim bindata() As Byte = dr.GetValue(1)
    Return bindata
  End Function
The method SaveImage accepts a byte array containing image data and saves it to the images table. The second method accepts image id to be retrieved and returns a byte array from the Web method.
Next, we will develop a Web client that presents UI for uploading files and calls this Web Service internally.
Creating the Client for the Web Service
Create a new Web application in VS.NET and add a Web reference to the Web Service we developed in the previous sections. Now, add a new