Sort the scripts,CodeSimple, but involves simple file Stream Conversion
As for the file path, remember to modify it. It passes debugging in. NET 2005 express beta.
<% @ WebService Language = " C # " Class = " Getbinaryfile " %>
Using System. Web;
Using System. Web. Services;
Using System. Web. Services. Protocols;
Using System. collections;
Using System. componentmodel;
Using System. Data;
Using System. diagnostics;
Using System. Web. UI;
Using System. IO;
[WebService (namespace = " Http: // localhost/webmp3downloads/ " , Description = " Using the dot. NET Framework in WebServices for file transmission " )]
Public Class Getbinaryfile: system. Web. Services. WebService {
[Webmethod]
Public String Helloworld () {
Return "Hello World";
}
[Webmethod (Description = " Provides the MP3 binary stream download service. " )]
Public Byte [] Getmp3file ( String Requestfilename)
{
// Obtain a file on the server.
If (Requestfilename = Null | Requestfilename = String . Empty)
{
ReturnGetbinaryfile ("D: \ 1.mp3");
}
Else
{
ReturnGetbinaryfile ("D :\\" +Requestfilename );
}
}
/**/ /// <Summary>
/// Convert a file to a binary stream convertfilestreamtobytebuffer
/// </Summary>
/// <Param name = "FILENAME"> </param>
/// <Returns> </returns>
Public Byte [] Getbinaryfile ( String Filename)
{
If (File. exists (filename ))
{
Try
{< br> // open an existing file for reading.
filestream S = file. openread (filename);
return This . convertstreamtobytebuffer (s);
}
Catch
{
Return New Byte[0];
}
}
Else
{
Return New Byte[0];
}
}
/**/ /// <Summary>
/// Convert a stream file to a binary byte array convert filestream into byte
/// </Summary>
/// <Param name = "thestream"> </param>
/// <Returns> </returns>
Public Byte [] Convertstreamtobytebuffer (system. Io. Stream thestream)
{
Int B1;
System. Io. memorystream tempstream = New System. Io. memorystream ();
While (B1 = Thestream. readbyte ()) ! = - 1 )
{
Tempstream. writebyte (((Byte) B1 ));
}
Return Tempstream. toarray ();
}
/**/ /// <Summary>
///Returns the file type of the transferred file.
/// </Summary>
/// <Returns> </returns>
[Webmethod (Description = " Type of the file to be transferred " )]
Public String Getfiletype ( String Filename)
{
Return "Text/txt";
}
}