I want to write a web service in c# that receives multiple files from the stream, read them into different files and then perform some ask on these files(say for example, the files that I need to send are XML):
This is what I've done so far:
1)Send the files by the following method, this creates a request and post it to the web service:
public void postMultipleFiles()
{
string url = "<the url of the web service>";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = true;
httpWebRequest.ContentType = "text/xml; encoding='utf-8'";
Stream memStream = new MemoryStream();
string[] files = { @"<file1 location>", @"<file2 location>" };
/*AJ*/
foreach (string file in files)
{
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
}
httpWebRequest.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
try
{
WebResponse webResponse = httpWebRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream);
response.InnerHtml = reader.ReadToEnd();
//Response.Write(reader.ReadToEnd());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
httpWebRequest = null;
}
2)Read the stream in the webservice as following:
[WebMethod]
public string MultiFilePost()
{
string xmls = "";
if (HttpContext.Current.Request.InputStream != null)
{
StreamReader stream = new StreamReader(HttpContext.Current.Request.InputStream);
xmls = stream.ReadToEnd(); // added to view content of input stream
xmls = xmls.Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
}
return xmls;
}
But this reads the file into a string, appended one after the other.
How do I manage to get this into two different XML files?
Thanks in advance....