简体   繁体   English

使用WCF REST上传文件

[英]Upload file using WCF REST

I am using following code : 我正在使用以下代码:

    static void test()
        {
            string address = "http://localhost:4700/HostDevServer/HelloWorldService.svc";
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);
            req.Method = "POST";
            req.ContentType = "text/xml;charset=UTF-8";
            Stream reqStream = req.GetRequestStream();
            string fileContents = "the quick brown fox jumped over the lazy dog.";
            byte[] bodyContents = Encoding.UTF8.GetBytes(fileContents);
            reqStream.Write(bodyContents, 0, bodyContents.Length);
            reqStream.Close();

            HttpWebResponse resp;
            try
            {
                **resp = (HttpWebResponse)req.GetResponse();**
            }
            catch (WebException e)
            {
                resp = (HttpWebResponse)e.Response;
            }
            Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription); 
        }



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MyWCFServices
{
    public class HelloWorldService : IHelloWorldService
    {
        public String GetMessage(String name)
        {
            return "Hello world from " + name + "!";
        }

        public bool UploadFile(Stream fileContent)
        {
            using (StreamReader fileContentReader = new StreamReader(fileContent))
            {
                string content = fileContentReader.ReadToEnd();
                File.WriteAllText(Path.Combine(@"c:\temp\aa.tmp"), content);
            }
            return false;
        }  
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.IO;
using System.Web;

namespace MyWCFServices
{
    [ServiceContract]
    interface IHelloWorldService
    {
        [OperationContract]
        String GetMessage(String name);

        [OperationContract]
        [WebInvoke(Method = "PUT", UriTemplate = "File/{fileName}")]
        //[WebContentType("application/octet-stream")]
        bool UploadFile(Stream fileContents); 

        //[OperationContract]
        //[WebInvoke(UriTemplate = "UploadFile/{fileName}")]
        //void UploadFile(string fileName, Stream fileContent); 
    }
}

While uploading file, i get an error of Unsopported media type at the code highlighted in bold. 上载文件时,在以粗体突出显示的代码处出现未支持的媒体类型错误。 Any idea ? 任何想法 ?

Although I can't directly see why it would affect this particular example - you shouldn't write the bytes of a file to a web request, because of the potential presence of byte values that are not expected or supported. 尽管我无法直接看到它为什么会影响此特定示例-您不应将文件的字节写入Web请求,因为可能存在不期望或不支持的字节值。

You should probably encode the bytes of the file as base 64 using 您可能应该使用以下代码将文件的字节编码为基数64

Convert.ToBase64String

Effectively uploading the file as a string. 有效地将文件上传为字符串。

At the other end you then treat the value as a string parameter on your service method, and use 在另一端,然后将值作为服务方法上的字符串参数使用,并使用

Convert.FromBase64String

To get the bytes, and then you have the original file again. 要获取字节,然后再次拥有原始文件。 At my company we have web services that we use to take binary files such as docx and this is how we do it - it works a treat. 在我的公司中,我们拥有用于获取二进制文件(例如docx Web服务,这就是我们的工作方式-它可以有效地解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM