This seemed to work for me

            var stream = new FileStream(@".\Files\test.txt", FileMode.Open, 
FileAccess.Read);
            var reader = new StreamReader(stream, Encoding.ASCII, true);
            
            return new ObjectResult(reader.BaseStream);


On Tuesday, 22 August 2017 10:00:01 UTC+1, Dombat wrote:
>
> I put this on Stackoverflow yesterday, but this is probably a better place 
> for it!
>
> Swagger has generated a server from an API with a method like this:
>
> [HttpGet]
>     [Route("SomeRoute")]
>     [SwaggerOperation("GetFile")]
>     [SwaggerResponse(200, type: typeof(System.IO.Stream))]
>     public virtual IActionResult GetFile()
>     {
>         string exampleJson = null;
>         string text = "This could be the contents of a file";
>
>         exampleJson = text;
>         var example = exampleJson != null
>         ? JsonConvert.DeserializeObject<System.IO.Stream>(exampleJson)
>         : default(System.IO.Stream);
>         return new ObjectResult(example);
>     }
>
> If I replace string text = "This could be the contents of a file" with 
> lines of code such as:
>
> var stream = new FileStream(@".\Files\test.txt", FileMode.Open, 
> FileAccess.Read);string text = new 
> StreamReader(stream,Encoding.ASCII,true).ReadToEnd();
>
> I get a JsonConvert exception saying "invalid characters" (because the 
> string is the content of the file and not json!) If I try and return a 
> stream (e.g. a fileStream) I get an exception too. 
>
>
> What is the correct way to return a stream from swagger generated code in 
> C#? 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to