[1.0] Using WebService to build a simple binary stream MP3 download service.

Source: Internet
Author: User

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";

}




}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.