Download files in a Nancy self-supporting application

I am working on a small project that uses Nancy hosted in a WPF application. I want to be able to remotely upload a ~ 8 MB PDF file. I managed to download the download, but during the download the application will not respond to any other requests. Is there a way to allow file download without linking all other requests?

Public Class ManualsModule : Inherits NancyModule Public Sub New() MyBase.New("/Manuals") Me.Get("/") = Function(p) Dim model As New List(Of String) From {"electrical", "opmaint", "parts"} Return View("Manuals", model) End Function Me.Get("/{name}") = Function(p) Dim manualName = p.name Dim fileResponse As New GenericFileResponse(String.Format("Content\Manuals\{0}.pdf", manualName)) Return fileResponse End Function End Sub End Class 

Or in C #

 public class ManualsModule : NancyModule { public ManualsModule() : base("/Manuals") { this.Get("/") = p => { List<string> model = new List<string> { "electrical", "opmaint", "parts" }; return View("Manuals", model); }; this.Get("/{name}") = p => { dynamic manualName = p.name; GenericFileResponse fileResponse = new GenericFileResponse(string.Format("Content\\Manuals\\{0}.pdf", manualName)); return fileResponse; }; } } 
+6
source share
4 answers

I found that I was actually accepting Nancy in the WCF, not myself. The behavior I talked about only happens when placed in WCF. Self-Host works fine for my application, so I will go with that.

+3
source
 var file = new FileStream(zipPath, FileMode.Open); string fileName = //set a filename var response = new StreamResponse(() => file, MimeTypes.GetMimeType(fileName)); return response.AsAttachment(fileName); 
+10
source

The easiest way is to create a StreamWriter around it, for example:

 var response = new Response(); response.Headers.Add("Content-Disposition", "attachment; filename=test.txt"); response.ContentType = "text/plain"; response.Contents = stream => { using (var writer = new StreamWriter(stream)) { writer.Write("Hello"); } }; return response; 
+2
source

Monivs answer works fine if you want the file to be attached, if you want to open the PDF directly in the browser, you can do it like this:

 MemoryStream ms = new MemoryStream(documentBody); var response = new Response(); response.ContentType = "application/pdf"; response.Contents = stream => { ms.WriteTo(stream); }; return response; 
0
source

Source: https://habr.com/ru/post/958559/


All Articles