2

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....

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    ASMX web services are all but obsolete. You should use WCF instead. Among other things, WCF has actual _support_ for file transfer and streaming. – John Saunders Feb 08 '13 at 07:24
  • 1
    Don't you need to use multipart form with a boundary? Like here: http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data – Tim Feb 08 '13 at 07:26
  • @Tim : yes I have now added the content type and its properties. But still how do I read it into different files in the web service where I receive these files –  Feb 08 '13 at 08:01

0 Answers0