Next, we will introduce how to download files from the server to the client through Web Services and upload files from the client to the server through Web Services.
1. Display and download files through Web Services
The Web Services created here are named GetBinaryFile, which provides two common methods: GetImage () and GetImageType (). The former returns the binary file byte array, and the latter returns the file type, the GetImage () method has a parameter used to select the name of the file to be displayed or downloaded on the client. The files we show and downloaded here can not be in the virtual directory. The advantage of this method is that the files can be displayed and downloaded according to the permissions. We can see from the following method that, the actual file location is not in the virtual directory, so you can better control the permissions of the file, which is particularly useful for high security. This function can be implemented using Stream objects in the previous ASP program. To facilitate testing, all the source code is listed and described and commented out in the source code.
First, create the GetBinaryFile. asmx file:
We can. create a C # aspxWebCS project in. NET, add a new project, select "Web service", and set the file name to GetBinaryFile. asmx: Enter the following code in "view code": GetBinaryFile. asmx. cs:
Copy codeThe Code is as follows: usingSystem;
UsingSystem. Collections;
UsingSystem. ComponentModel;
UsingSystem. Data;
UsingSystem. Diagnostics;
UsingSystem. Web;
UsingSystem. Web. UI;
UsingSystem. Web. Services;
UsingSystem. IO;
Namespacexml.sz.luohuedu.net. aspxWebCS
{
/// <Summary>
/// Summary of GetBinaryFile.
/// WebServices name: GetBinaryFile
/// Function: return the binary byte array of a file object on the server.
/// </Summary>
[WebService (Namespace = "http://xml.sz.luohuedu.net /",
Description = "Use the. NET Framework in WebServices to transmit binary files. ")]
PublicclassGetBinaryFile: System. Web. Services. WebService
{
# RegionComponentDesignergeneratedcode
// Required by the Web Service designer
PrivateIContainercomponents = null;
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protectedoverrisponiddispose (booldisposing)
{
If (disposing & components! = Null)
{
Components. Dispose ();
}
Base. Dispose (disposing );
}
# Endregion
PublicclassImages: System. Web. Services. WebService
{
/// <Summary>
/// The method provided by the Web service, returns the byte array of the given file.
/// </Summary>
[WebMethod (Description = "the method provided by the Web service, returns the byte array of the given file")]
Publicbyte [] GetImage (stringrequestFileName)
{
/// Get an image of the server
/// If you test it by yourself, make sure to modify the actual physical path below
If (requestFileName = null | requestFileName = "")
ReturngetBinaryFile ("D: \ Picture. JPG ");
Else
ReturngetBinaryFile ("D :\\" + requestFileName );
}
/// <Summary>
/// GetBinaryFile: returns the byte array of the given file path.
/// </Summary>
/// <Paramname = "filename"> </param>
/// <Returns> </returns>
Publicbyte [] getBinaryFile (stringfilename)
{
If (File. Exists (filename ))
{
Try
{
/// Open an existing file for reading.
FileStreams = File. OpenRead (filename );
ReturnConvertStreamToByteBuffer (s );
}
Catch (effectione)
{
Returnnewbyte [0];
}
}
Else
{
Returnnewbyte [0];
}
}
/// <Summary>
/// ConvertStreamToByteBuffer: convert a given file flow into a binary byte array.
/// </Summary>
/// <Paramname = "theStream"> </param>
/// <Returns> </returns>
Publicbyte [] ConvertStreamToByteBuffer (System. IO. StreamtheStream)
{
Intb1;
System. IO. MemoryStreamtempStream = newSystem. IO. MemoryStream ();
While (b1 = theStream. ReadByte ())! =-1)
{
TempStream. WriteByte (byte) b1 ));
}
ReturntempStream. ToArray ();
}
[WebMethod (Description = "method provided by the Web service, returns the given file type. ")]
PublicstringGetImageType ()
{
/// This is only a test. You can perform dynamic output based on the actual file type.
Return "image/jpg ";
}
}
}
}