代码之家  ›  专栏  ›  技术社区  ›  FllnAngl

将XML复制到没有头的新文件

  •  0
  • FllnAngl  · 技术社区  · 8 年前

    我正在尝试获取已发送到我的应用程序的XML文件的正文数据。

    但当我复制内容并将其放在新文件中时,内容是不正确的。

    原始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>
    

    粘贴正文后的新文件:

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

    我只希望第二个文件也包含 xml 而不是标题数据…(至少我认为是标题数据)

    我到底怎么解决这个问题?


    到目前为止我得到的是:

            //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;
            }
    

    到目前为止我所做的:

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

    0 回复  |  直到 8 年前