Igor Kulman

Returning files in NancyFX

· Igor Kulman

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