0

我已经(试图)自定义我在 Stackoverflow 上找到的方法,以启用一个 XML 文件和多个图像的多部分上传,以从 Sharepoint WebPart 处理 HTTPWebRequest。不好意思,我不得不说我真的不熟悉 HttpRequest 和 StreamUpload 的事情,这是我第一次认真尝试处理这个问题。

这是我的编码:

    public XmlDocument DoHttpUploadFile(string url, string[] file, string[] paramName, string[] contentType, NameValueCollection nvc, NameValueCollection headerItems, string xmlFileName)
    {
        var xmlDox = new XmlDocument();
        var totalLength = 0;
        string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
        byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
        const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";

        // First Loop is just need to get the Length of all Parts

        for (int i = 0; i < file.Length; i++)
        {
            string header = "";
            if (i == 0)
            {
                header = string.Format(headerTemplate, paramName[i], xmlFileName, contentType[i]);
            }
            else
            {
                var filename = Path.GetFileName(file[i]);
                header = string.Format(headerTemplate, paramName[i], filename, contentType[i]);
            }

            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

            var headerLength = headerbytes.Length;
            var boundaryLength = boundarybytes.Length;

            totalLength = totalLength + headerLength + boundaryLength;

            if (file[i].StartsWith("<"))
            {
                byte[] xmlBytes = System.Text.Encoding.UTF8.GetBytes(file[i]);
                var xmlLength = xmlBytes.Length;
                totalLength = totalLength + xmlLength;
            }
            else
            {
                var client = new WebClient();
                var img = new Image { ImageUrl = file[i] };

                client.UseDefaultCredentials = true;
                // image as Byte into Stream
                var fileData = client.DownloadData(img.ImageUrl);
                var imgLength = fileData.Length;
                totalLength = totalLength + imgLength;
            }
        }

        var wr = (HttpWebRequest)WebRequest.Create(url);

        wr.ContentType = "multipart/form-data; boundary=" + boundary;
        wr.Method = "POST";
        wr.ContentLength = totalLength;

        Stream rs = wr.GetRequestStream();
        var read = 0;
        var finalLength = 0;

        for (int i = 0; i < file.Length; i++)
        {
            string header = "";
            if(i==0)
            {
                header = string.Format(headerTemplate, paramName[i], xmlFileName, contentType[i]);
            }
            else
            {
                var filename = Path.GetFileName(file[i]);
                header = string.Format(headerTemplate, paramName[i], filename, contentType[i]);
            }

            rs.Write(boundarybytes, 0, boundarybytes.Length);

            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

            var headerLength = headerbytes.Length;
            var boundaryLength = boundarybytes.Length;
            finalLength = finalLength + headerLength + boundaryLength;
            rs.Write(headerbytes, 0, headerbytes.Length);

            if(file[i].StartsWith("<"))
            {
                var writer = new StreamWriter(rs);
                byte[] xmlBytes = System.Text.Encoding.UTF8.GetBytes(file[i]);
                var xmlLength = xmlBytes.Length;
                finalLength = finalLength + xmlLength;
                writer.Write(file[i]);
            }
            else
            {
                var getitem = new GetItemFromList();

                var client = new WebClient();
                var img = new Image {ImageUrl = file[i]};

                client.UseDefaultCredentials = true;
                // image as Byte into Stream
                var fileData = client.DownloadData(img.ImageUrl);
                finalLength = finalLength + fileData.Length;
                rs.Write(fileData, 0, fileData.Length);
            }

        }

        finalLength.ToString();
rs.Close(); // Here it stops with Exception 'Bytes are not completely written



        WebResponse wresp = null;
        try
        {
            wresp = wr.GetResponse();
            var wrlength = wr.ContentLength;
            var stream2 = wresp.GetResponseStream();
            if (stream2 != null)
            {
                var reader2 = new StreamReader(stream2);
                var backstr = reader2.ReadToEnd();

                xmlDox.LoadXml(backstr);
            }
        }
        catch (Exception ex)
        {
            //log.Error("Error uploading file", ex);
            if (wresp != null) 
                wresp.Close();
            wresp = null;
        }
        finally
        {
            wr = null;            }

        return xmlDox;
    }

它在 rs.Close 处停止,并出现异常“无法关闭流字节未写入完成”。

我想这可能取决于 wr.ContentLength 并尝试手动设置它,正如您在上面的编码部分中看到的那样。

我找不到解决方案,我在前两天尝试了并在网上搜索了所有内容,尤其是 stackeOverflow。

所以,如果有人能告诉我出了什么问题,请告诉我,谢谢。

4

1 回答 1

0

“抛出异常是因为您写入的字节数少于 WebRequest 预期的字节数。例如,如果您在 ContentLength 属性中设置了 75 个字节,并且您在 ResquestStream 上写入了 69 个字节并关闭它,则会引发异常。”

请参阅此帖子:在写入所有字节之前无法关闭流

您能否检查 TotalLength 的大小以确保您使用的是字节长度并在调试器中进行验证?

于 2012-10-02T13:50:11.253 回答