I have read a number of answers, and been searching the web for the last few weeks to try and find an answer to this.
I have read: How to return a file (FileContentResult) in ASP.NET WebAPI
and: Returning binary file from controller in ASP.NET Web API
When I am debugging, it will just continuously callback, running the method over and over.
When running on a server (IIS), depending on the browser, there will be a message saying This site can’t be reached
, or it will download the file, but indicate that the download has faield.
The code I am using is
<HttpGet, Route("api/Reports/Create")>
Public Function CreateReport() As HttpResponseMessage
Dim data = GetReportBuffer()
'This is just for testing
'To check that the report is created correctly
'I can open up temp.pdf, it is fine
Using fs As New FileStream("c:\temp\temp.pdf", FileMode.Create)
data.CopyTo(fs)
fs.Flush()
fs.Close()
End Using
Dim result = Request.CreateResponse(HttpStatusCode.OK)
'This should be StreamContent(data... but using this for testing
result.Content = New StreamContent(New FileStream("c:\temp\temp.pdf", FileMode.Open))
result.Content.Headers.ContentType = New MediaTypeHeaderValue("application/octet-stream")
result.Content.Headers.ContentDisposition = New ContentDispositionHeaderValue("attachment") With {.FileName = $"{fllId}.{ext}"}
Return result
End Function
I have also tries ByteArrayContent, but the result is exactly the same. Is there anything I am not doing correctly?