Hello All,
I am implementing WCF REST service to accept .gzip file. We are able to receive .txt.and .xml file but when client sends the .gz file we are getting invalid stream or garbage data. we are not able to convert received stream into .gz file again.
Clinet sending gzip compressed ASCII file to service as HTTP POST method.
IService
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "processfile",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
string processfile(Stream input);
Service :
public string processfile(Stream input)
{
string saveToLog1 = @"E:\TestFiles\LogFilett.txt";
compress(input);
byte[] input1 = ReadFully(input);
string output1 = Encoding.UTF8.GetString(input1, 0, input1.Length);
File.WriteAllText(saveToLog1, output1);
string saveToLog = @"E:\TestFiles\LogFile.txt";
File.AppendAllText(saveToLog, "File received at: " + System.DateTime.Now.ToString().Replace(":", "_").Replace("/", "-").Replace(" ", "_"));
string saveTo = @"E:\TestFiles\Testtest.gz";
File.WriteAllText(saveTo, output1, UTF8Encoding.UTF8);
return string.Format("The input string is: {0}", DateTime.Now.ToString());
}
ReadFully Method
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
Thank you in advance