tomkuijsten / restup

Webserver for universal windows platform (UWP) apps
MIT License
114 stars 48 forks source link

GetReponse return binary file #129

Open arvin-a opened 7 years ago

arvin-a commented 7 years ago

Is there any way to make GetResponse to return a binary file? I tried the following but it didn't work.

StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
                StorageFile file = await storageFolder.GetFileAsync("myimage.jpg");
                if (file != null)
                {
                    var stream = await file.OpenStreamForReadAsync();
                    var bytes = new byte[(int) stream.Length];
                    stream.Read(bytes, 0, (int) stream.Length);

                    IReadOnlyDictionary<string, string> header = new Dictionary<string, string>
                    {
                        {"Content-Type", "image/jpeg"},
                        {"Content-Disposition", "myimage.jpg"},
                        {"Accept-Ranges", "bytes"},
                        {"Content-Length", stream.Length.ToString()}
                    };
                    return new GetResponse(
                        GetResponse.ResponseStatus.OK,
                        header,
                        bytes
                    );
tomkuijsten commented 7 years ago

Not by default, no. You could implement your own handler.

arvin-a commented 7 years ago

I want to implement a rest route that will receive a request like /api/fileid/112 and return a binary file to the user to download. Would that still be done by implementing IRouteHandler?

tomkuijsten commented 7 years ago

Jup. The default handlers uses json or xml (based on the http request headers) as return type, doesn't support binary.