正在从流中读取DOCX文件



我编写了以下代码,用于将两个文件(一个XML文件和一个DOCX文件)发布到Web服务中:

public void postMultipleFiles(string url, string[] files)
    {
        string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
        httpWebRequest.Method = "POST";
        httpWebRequest.KeepAlive = true;
        httpWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
        Stream memStream = new System.IO.MemoryStream();
        byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("rn--" + boundary + "rn");
        string formdataTemplate = "rn--" + boundary + "rnContent-Disposition: form-data; name="{0}";rnrn{1}";
        string headerTemplate = "Content-Disposition: form-data; name="{0}"; filename="{1}"rn Content-Type: application/octet-streamrnrn";
        memStream.Write(boundarybytes, 0, boundarybytes.Length);
        for (int i = 0; i < files.Length; i++)
        {
            string header = string.Format(headerTemplate, "file" + i, files[i]);
            //string header = string.Format(headerTemplate, "uplTheFile", files[i]);
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            memStream.Write(headerbytes, 0, headerbytes.Length);
            FileStream fileStream = new FileStream(files[i], 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);
            }
            memStream.Write(boundarybytes, 0, boundarybytes.Length);
            fileStream.Close();
        }
        /*AJ*/
        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();
        }
        catch (Exception ex)
        {
            response.InnerHtml = ex.Message;
        }
        httpWebRequest = null;
    }

在Web服务中,我检索到如下内容:

public string MultiFilePost()
{
    string xmls = "";
    string txt = "";
    XmlDocument xmldoc = new XmlDocument();
    if (HttpContext.Current.Request.InputStream != null)
    {
        StreamReader stream = new StreamReader(HttpContext.Current.Request.InputStream);
        /*First File*/
        Stream xmlStream = System.Web.HttpContext.Current.Request.Files[0].InputStream;
        StreamReader rd = new StreamReader(xmlStream);
        xmls = rd.ReadToEnd();
        /*Second File*/
        Stream txtStream = System.Web.HttpContext.Current.Request.Files[1].InputStream;
        rd = new StreamReader(txtStream);
        txt = rd.ReadToEnd();
    }
    return xmls;
}

因此,我得到了字符串的输入流。我可以将字符串"xmls"转换为XmlDocument对象,但在字符串txt中,我会收到某种字符串,我需要一种方法来处理它,就像XmlDocument处理XML文件一样。

提前谢谢。

使用XmlDocument.Load方法(流):

// First file
var xmlStream = System.Web.HttpContext.Current.Request.Files[0].InputStream;
var xmlDocument = new XmlDocument();
xmlDocument.Load(xmlStream);
// The same code for the second file processing.
// Second file
var wordDocStream = System.Web.HttpContext.Current.Request.Files[1].InputStream;
var wordXmlDocument = new XmlDocument();
wordXmlDocument.Load(wordDocStream);

更新

使用Word文档:

  • OpenXML
  • 关闭的XML

相关内容

  • 没有找到相关文章

最新更新