0

I am trying to get the body data of an Xml file that's been sent to my application.

but when I copy the contents and place it in a new file, the content is incorrect.

Original Xml:

<?xml version="1.0" encoding="utf-8"?>
<eM_NM_001 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <MESSAGE>
    ....
  </MESSAGE>
</eM_NM_001>

New file after the body gets pasted:

-----------------------8d618ce02a1a097
Content-Disposition: form-data; name="file"; filename="14_20180912162639.xml"
Content-Type: application/octet-stream

<?xml version="1.0" encoding="utf-8"?>
<eM_NM_001 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <MESSAGE>
    ...
  </MESSAGE>
</eM_NM_001>
-----------------------8d618ce02a1a097--

I just want the second file to also contain xml and not the Header data...(atleast I think it's the header data)

How on earth do I solve this?


What I've got so far:

        //POST api/<controller>
        [HttpPost]
        public string Post()
        {
            var result = "";
            if (Request.Content != null)
            {
                string map = @"D:\bestelApp\bestelApp\Content\bestellingen\ontvangen";
                string count = Directory.GetFiles(map, "*.xml").Count().ToString();
                string extention = ".xml";
                string fileName = "bestelling" + (count != "0" ? count : "") + extention;

                string path = map + "//" + fileName;

                using (Stream output = File.OpenWrite(path))
                {
                    using (Stream input = HttpContext.Current.Request.GetBufferedInputStream())
                    {
                        input.CopyTo(output);
                    }
                }

                result = new bestellingenController().ConvertXmlToObj(fileName, path);
            }
            return result;
        }

What I've tried so far:

[HttpPost]
public void Post(HttpRequestMessage request) {
    var doc = new XmlDocument();
    doc.Load(request.Content.ReadAsStreamAsync().Result);
    doc.Save(@"D:\bestelApp\bestelApp\Content\bestellingen\ontvangen");
}

var context = new HttpContextWrapper(HttpContext.Current);
context.Request.ContentType = null;
HttpRequestBase request = context.Request; //This was an attempt to set everything to null except the xml.

//Get the data from the HTTP stream
var body = new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();

var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(body);
xmlDocument.Save(@"D:\bestelApp\bestelApp\Content\bestellingen\ontvangen\" + fileName);

I had already asked this question but poorly..

FllnAngl
  • 556
  • 1
  • 5
  • 30

0 Answers0