本文介绍了WCF 4.0 REST上传MS-Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过WCF-REST服务上传MS-Excel文件。
我使用了下面的帖子给出的解决方案: -

我的POST方法被声明为:

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

当我调试时,流的内容是好的,直到调用去,当我附加服务调试,流fileContents参数变为空,服务返回[Bad Request]。我没有发送大文件(这只是50 KB)。我使用HttpClient来调用Post。
这里是客户端代码(RestClient是HttpClient)。

pre $ protected $ Post(string uri,Stream stream,int长度)
{
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);
}

任何线索都可能出错。
很多感谢。

解决方案

I am trying to upload MS-Excel file through WCF-REST Service.I used the solution given in below post:-RESTful WCF service image upload problemMy POST Method is declared as:

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

When I debug, stream content is fine till the call goes, and when I attach service to debug, Stream fileContents parameter becomes null , and service returns with [Bad Request]. I am not sending large file (it is just 50 KB). I am using HttpClient to call the Post.Here are the client code(RestClient is 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);
        }

Any clue what else can go wrong.Many Thanks.

解决方案

[WebContentType("application/octet-stream")] attribute was unnecessary here. I commented it out, and all worked fine :).

这篇关于WCF 4.0 REST上传MS-Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:27