76 lines
2.1 KiB
C#
76 lines
2.1 KiB
C#
using System;
|
|
using System.Web;
|
|
using System.Web.Services;
|
|
using System.Web.Services.Protocols;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Xml.Serialization;
|
|
using System.IO.Compression;
|
|
|
|
[WebService(Namespace = "http://www.maolaoda.com/CRAXFileTransfer")]
|
|
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
|
|
public class CRAXFileTransfer : System.Web.Services.WebService
|
|
{
|
|
public CRAXFileTransfer()
|
|
{
|
|
//Uncomment the following line if using designed components
|
|
//InitializeComponent();
|
|
}
|
|
|
|
[WebMethod]
|
|
public GetFileResponse GetFile(string filename, bool compress)
|
|
{
|
|
GetFileResponse response = new GetFileResponse();
|
|
try
|
|
{
|
|
response.FileName = filename;
|
|
|
|
if (compress)
|
|
{
|
|
FileStream readStream = File.Open(Server.MapPath(filename), FileMode.Open, FileAccess.Read);
|
|
byte[] buffer = new byte[readStream.Length];
|
|
int count = readStream.Read(buffer, 0, buffer.Length);
|
|
readStream.Close();
|
|
if (count != buffer.Length)
|
|
{
|
|
response.Message = "Unable to read data from " + filename;
|
|
}
|
|
else
|
|
{
|
|
MemoryStream outputStream = new MemoryStream();
|
|
GZipStream compressStream = new GZipStream(outputStream, CompressionMode.Compress, true);
|
|
compressStream.Write(buffer, 0, buffer.Length);
|
|
compressStream.Flush();
|
|
compressStream.Close();
|
|
response.FileContents = outputStream.GetBuffer();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
response.FileContents = File.ReadAllBytes(Server.MapPath(filename));
|
|
}
|
|
|
|
response.Message = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
response.Message = "ERROR:" + ex.Message;
|
|
}
|
|
return response;
|
|
}
|
|
|
|
[XmlRoot("getFileResponse", Namespace = "http://www.maolaoda.com/CRAXFileTransfer")]
|
|
public class GetFileResponse
|
|
{
|
|
[XmlElement("fileName", IsNullable = false)]
|
|
public string FileName;
|
|
|
|
[XmlElement("message", IsNullable = false)]
|
|
public string Message;
|
|
|
|
[XmlElement("fileData", IsNullable = true)]
|
|
public byte[] FileContents;
|
|
}
|
|
|
|
}
|