Upload a file using WCF Rest?

If there is a way to upload a file using rest through the stream , will there also be a " Download"? If so, could you tell me how to do this? Thanks in advance!

+6
source share
2 answers

A sample method that I use to download a file from my REST service:

[WebGet(UriTemplate = "file/{id}")]
        public Stream GetPdfFile(string id)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/txt";
            FileStream f = new FileStream("C:\\Test.txt", FileMode.Open);
            int length = (int)f.Length;
            WebOperationContext.Current.OutgoingResponse.ContentLength = length;
            byte[] buffer = new byte[length];
            int sum = 0;
            int count;
            while((count = f.Read(buffer, sum , length - sum)) > 0 )
            {
                sum += count;
            }
            f.Close();
            return new MemoryStream(buffer); 
        }
+8
source

You can also use the following

 public Stream GetFile(string id)
 {
      WebOperationContext.Current.OutgoingResponse.ContentType = "application/txt";
      var byt = File.ReadAllBytes("C:\\Test.txt");
      WebOperationContext.Current.OutgoingResponse.ContentLength = byt.Length;
      return new MemoryStream(byt);
 }

when it is defined as

[WebGet(UriTemplate = "file/{id}")]
[OperationContract]
Stream GetPdfFile(string id);
0
source

All Articles