Note that there are some explanatory texts on larger screens.

plurals
  1. POUpload file using WCF REST
    primarykey
    data
    text
    <p>I am using following code :</p> <pre><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); } } </code></pre> <p>While uploading file, i get an error of Unsopported media type at the code highlighted in bold. Any idea ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload