【问题标题】:WCF 4.0 REST Upload MS-Excel FileWCF 4.0 REST 上传 MS-Excel 文件
【发布时间】:2011-06-11 03:52:51
【问题描述】:

我正在尝试通过 WCF-REST 服务上传 MS-Excel 文件。 我使用了下面帖子中给出的解决方案:- RESTful WCF service image upload problem 我的 POST 方法声明为:

  [OperationContract]
                [WebInvoke(Method = "POST", UriTemplate = "/RFQ")]
                [WebContentType("application/octet-stream")]
                void UploadRFQDoc(Stream fileContents);

当我调试时,流内容很好,直到调用结束,当我将服务附加到调试时,Stream fileContents 参数变为 null ,并且服务返回 [Bad Request]。我没有发送大文件(只有 50 KB)。我正在使用 HttpClient 来调用 Post。 这是客户端代码(RestClient 是 HttpClient)。

 protected void Post(string uri, Stream stream, int length)
        {
            var content = HttpContent.Create(output => CopyToStream(stream, output, length), "application/octet-stream", length);
            Uri relativeUri = new Uri(uri, UriKind.Relative);

            var resp = RestClient.Post(relativeUri, content);

            ProcessResponse(resp);
        }

        void CopyToStream(Stream input, Stream output, int length)
        {
            var buffer = new byte[length];
            var read = input.Read(buffer, 0, Convert.ToInt32 (length));

            output.Write(buffer, 0, read);
        }

任何其他可能出错的线索。 非常感谢。

【问题讨论】:

    标签: wcf rest file-upload


    【解决方案1】:

    [WebContentType("application/octet-stream")] 属性在这里是不必要的。我把它注释掉了,一切都很好:)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多