在httpwebrequest期间,什么可能导致.docx文件中的这种损坏?

时间:2017-02-08 00:09:09

标签: .net httpwebrequest docx corruption

我正在使用httpwebrequest将一些其他表单数据从MVC应用程序发布到经典ASP站点。

如果文件是.docx,它总是以损坏的方式到达。其他人似乎开得很好,但可能是他们的格式更灵活。

当我在Sublime Text中打开原始文件和损坏的文件时,我注意到损坏的文件最后缺少0000块。当我手动替换此块时,文件打开正常。

enter image description here

我的.NET代码中是否有错误导致这种情况发生?或者这个问题更深奥?

经典ASP代码使用Persist's AspUpload来接收文件。这在接收站点的其他地方的许多地方使用,并且以前从未引起任何问题。所以我不认为错误就在那里。另外,这只是一个简单的电话,我看不出有什么问题!

Set File = Upload.Files("fileField")

我对如何进一步开始调试此问题感到很茫然。

这是我用来发布文件的代码:

public async Task<string> TestFileSend()
{
    string result;

    var postToUrl = "https://www.mywebsite.com/receive-file.asp";

    Dictionary<string, string> extraData = new Dictionary<string, string>();
    extraData.Add("colour", "red");
    extraData.Add("name", "sandra");

    var filePath = "/path-to-file/file.docx";
    byte[] fileAsByteArray = File.ReadAllBytes(filePath);


    // setup data  to send
    var dataBoundry = "---------------------------9849436581144108930470211272";
    var dataBoundryAsBytes = Encoding.ASCII.GetBytes(Environment.NewLine + "--" + dataBoundry + Environment.NewLine);

    var startOfFileData = "--" + dataBoundry + Environment.NewLine +
        @"Content-Disposition: form-data; name=""fileField""; filename=""file.docx""" + Environment.NewLine;

    startOfFileData += @"Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document" + Environment.NewLine + Environment.NewLine;
    var startOfFileDataAsBytes = Encoding.UTF8.GetBytes(startOfFileData);
    var endOfRequest = "--" + dataBoundry + "--";
    byte[] endOfRequestAsBytes = Encoding.UTF8.GetBytes(endOfRequest);


    // perform request
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(postToUrl);
    httpWebRequest.ContentType = "multipart/form-data; boundary=" + dataBoundry;
    httpWebRequest.Method = "POST";
    using (var stream = await httpWebRequest.GetRequestStreamAsync())
    {
        foreach (KeyValuePair<string, string> item in extraData)
        {
            var dataItemBytes = DataItemAsBytes(item.Key, item.Value);
            stream.Write(dataBoundryAsBytes, 0, dataBoundryAsBytes.Length);
            stream.Write(dataItemBytes, 0, dataItemBytes.Length);
        }
        stream.Write(startOfFileDataAsBytes, 0, startOfFileDataAsBytes.Length);
        stream.Write(fileAsByteArray, 0, fileAsByteArray.Length);
        stream.Write(endOfRequestAsBytes, 0, endOfRequestAsBytes.Length);
    }
    try
    {
        using (WebResponse response = httpWebRequest.GetResponse())
        {
            HttpWebResponse httpResponse = (HttpWebResponse)response;
            using (Stream myData = response.GetResponseStream())
            using (var reader = new StreamReader(myData))
            {
                result = reader.ReadToEnd();
            }
        }
    }
    catch (WebException e)
    {
        result = e.Message;
    }

    return result;
}

解决了问题 - 这是修订后的工作代码

乔恩对他的回答很开心。我在写完文件流后立即添加了他建议的行,现在他们转移没有任何问题。

public async Task<string> TestFileSend()
{
    string result;

    var postToUrl = "https://www.mywebsite.com/receive-file.asp";

    Dictionary<string, string> extraData = new Dictionary<string, string>();
    extraData.Add("colour", "red");
    extraData.Add("name", "sandra");

    var filePath = "/path-to-file/file.docx";
    byte[] fileAsByteArray = File.ReadAllBytes(filePath);


    // setup data  to send
    var dataBoundry = "---------------------------9849436581144108930470211272";
    var dataBoundryAsBytes = Encoding.ASCII.GetBytes(Environment.NewLine + "--" + dataBoundry + Environment.NewLine);

    var startOfFileData = "--" + dataBoundry + Environment.NewLine +
        @"Content-Disposition: form-data; name=""fileField""; filename=""file.docx""" + Environment.NewLine;

    startOfFileData += @"Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document" + Environment.NewLine + Environment.NewLine;
    var startOfFileDataAsBytes = Encoding.UTF8.GetBytes(startOfFileData);
    var endOfRequest = "--" + dataBoundry + "--";
    byte[] endOfRequestAsBytes = Encoding.UTF8.GetBytes(endOfRequest);


    // perform request
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(postToUrl);
    httpWebRequest.ContentType = "multipart/form-data; boundary=" + dataBoundry;
    httpWebRequest.Method = "POST";
    using (var stream = await httpWebRequest.GetRequestStreamAsync())
    {
        foreach (KeyValuePair<string, string> item in extraData)
        {
            var dataItemBytes = DataItemAsBytes(item.Key, item.Value);
            stream.Write(dataBoundryAsBytes, 0, dataBoundryAsBytes.Length);
            stream.Write(dataItemBytes, 0, dataItemBytes.Length);
        }
        stream.Write(startOfFileDataAsBytes, 0, startOfFileDataAsBytes.Length);
        stream.Write(fileAsByteArray, 0, fileAsByteArray.Length);
        // *** THIS ADDITIONAL LINE IS THE KEY 
        stream.Write(new byte[] { 45, 45 }, 0, 2);
        // ***
        stream.Write(endOfRequestAsBytes, 0, endOfRequestAsBytes.Length);
    }
    try
    {
        using (WebResponse response = httpWebRequest.GetResponse())
        {
            HttpWebResponse httpResponse = (HttpWebResponse)response;
            using (Stream myData = response.GetResponseStream())
            using (var reader = new StreamReader(myData))
            {
                result = reader.ReadToEnd();
            }
        }
    }
    catch (WebException e)
    {
        result = e.Message;
    }

    return result;
}

1 个答案:

答案 0 :(得分:1)

我最近玩过multipart / form-data并发现它在最终多部分边界的末尾有一个额外的–-。这个stackoverflow answer有一个例子。我认为就是你丢失两个字节的地方。

如果是这样,解决方案是将最终写入添加到45个字节(ASCII - )的两个字节的请求流中。

stream.Write(new byte[]{45, 45}, 0, 2);

我无法确定,但它看起来很合适。希望它有所帮助。