Returning files in NancyFX
In my current project, I have chosen NancyFx to implement a REST API. NancyFx is a .NET framework known for its “super-duper-happy-path“.
In one use case I generate a ZIP file in the temp folder and I want the API to return this ZIP file. NancyFx contains a Responses.AsFile helper but it works only with paths relative to the application.
If you have an absolute path of a file, you cannot use it. You need to create a StreamResponse and return the file as an attachment
var file = new FileStream(zipPath, FileMode.Open);
string fileName = //set a filename
var response = new StreamResponse(() => file, MimeTypes.GetMimeType(fileName));
return response.AsAttachment(fileName);
See also
- Using ETag to cache responses in NancyFX
- NancyFX authentication for REST API
- Changing the PivotItem header color in Windows Phone 8.1 XAML
- Dialog helper for Universal Apps the easy way
- Creating a fake splashscreen for your Universal App