整理了一下下,代码很简单,只是涉及了简单的文件流转化
至于文件路径请记得修改.在.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 = "在WebServices里利用Dot.Net框架进行文件传输")]
public class GetBinaryFile : System.Web.Services.WebService {
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod(Description ="提供MP3的2进制流下载服务")]
public byte[] GetMp3File(string requestFileName)
{
//得到服务器端的一个文件
if (requestFileName == null||requestFileName==string.Empty)
{
return getBinaryFile("D:\\1.mp3");
}
else
{
return getBinaryFile("D:\\" + requestFileName);
}
}
/// <summary>
/// 将文件转化成2进制流 ConvertFileStreamToByteBuffer
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public byte[] getBinaryFile(string filename)
{
if (File.Exists(filename))
{
try
{
//打开现有文件以进行读取。
FileStream s = File.OpenRead(filename);
return this.ConvertStreamToByteBuffer(s);
}
catch
{
return new byte[0];
}
}
else
{
return new byte[0];
}
}
/// <summary>
/// 将流文件转化成2进制字节数组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>
/// 返回传送文件的文件类型 Get File Type of the Specific File
/// </summary>
/// <returns></returns>
[WebMethod(Description = "返回传送文件的文件类型")]
public string GetFileType(string fileName)
{
return "Text/txt";
}
}
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 = "在WebServices里利用Dot.Net框架进行文件传输")]
public class GetBinaryFile : System.Web.Services.WebService {
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod(Description ="提供MP3的2进制流下载服务")]
public byte[] GetMp3File(string requestFileName)
{
//得到服务器端的一个文件
if (requestFileName == null||requestFileName==string.Empty)
{
return getBinaryFile("D:\\1.mp3");
}
else
{
return getBinaryFile("D:\\" + requestFileName);
}
}
/// <summary>
/// 将文件转化成2进制流 ConvertFileStreamToByteBuffer
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public byte[] getBinaryFile(string filename)
{
if (File.Exists(filename))
{
try
{
//打开现有文件以进行读取。
FileStream s = File.OpenRead(filename);
return this.ConvertStreamToByteBuffer(s);
}
catch
{
return new byte[0];
}
}
else
{
return new byte[0];
}
}
/// <summary>
/// 将流文件转化成2进制字节数组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>
/// 返回传送文件的文件类型 Get File Type of the Specific File
/// </summary>
/// <returns></returns>
[WebMethod(Description = "返回传送文件的文件类型")]
public string GetFileType(string fileName)
{
return "Text/txt";
}
}