WCF 将一个字节 [] 发布到服务



我正在尝试将 byte[] 或 FromBase64String 字符串发布到我的网络服务。我不断收到 404 错误。我已将["内容类型"]更改为"文本/纯文本",但仍然收到错误。但是,当我注释掉字节数组时,一切都很好。有人有建议吗?

客户:

      Object _object = new Object
        {
            Param1String = "data",
            Param2String= "data",           
            Param3String = "data",
            ByteArray = ReadAllBytes(@"C:Foldertest.pdf")
        };
        try
        {
            WebClient Proxy = new WebClient();                
            Proxy.Headers["Content-type"] = "application/json";                
            MemoryStream stream = new MemoryStream();
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Object));
            serializer.WriteObject(stream, _object);
            byte[] data = Proxy.UploadData("http://localhost:4589/TestService.svc/FileUpload", "POST", stream.ToArray());
            stream = new MemoryStream(data);
            serializer = new DataContractJsonSerializer(typeof(Object));
            var result = serializer.ReadObject(stream) as Object;
            Console.WriteLine(result.Param1String);
            Console.ReadKey(true);
        }
        catch (Exception ex)
        {
        }

服务:

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/FileUpload")]
    void FileUpload(Patron patron);
    public void FileUpload(Object patron)
    {
            byte[] newBytes = null;
            BinaryWriter Writer = null;
            string Name = @"C:\Temp\" + object.Param1String + "";
            //byte[] bData = Helper.CopyToArray(object.ByteArray);
            try
            {
                newBytes = Convert.FromBase64String(patron.ByteArray);
                Writer = new BinaryWriter(File.OpenWrite(Name));                    
                Writer.Write(newBytes);
                Writer.Flush();
                Writer.Close();
            }
            catch
            {                    
                //return false;
            }
        }

在序列化对象之前,您需要对文件内容进行 base64 编码。 并将其转换回另一侧的 byte[]

Object _object = new Object
    {
        Param1String = "data",
        Param2String= "data",           
        Param3String = "data",
        Base64ByteArray =  Convert.ToBase64String(ReadAllBytes(@"C:Foldertest.pdf"))
    };

相关内容

  • 没有找到相关文章

最新更新