Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not Receiving Data from Route C#

I'm attempting to return an image from a server route, but I'm getting one that is 0 bytes. I suspect it has something to do with how I'm using the MemoryStream. Here's my code:

[HttpGet]
[Route("edit")]
public async Task<HttpResponseMessage> Edit(int pdfFileId)
{
    var pdf = await PdfFileModel.PdfDbOps.QueryAsync((p => p.Id == pdfFileId));

    IEnumerable<Image> pdfPagesAsImages = PdfOperations.PdfToImages(pdf.Data, 500);
    MemoryStream imageMemoryStream = new MemoryStream();
    pdfPagesAsImages.First().Save(imageMemoryStream, ImageFormat.Png);

    HttpResponseMessage response = new HttpResponseMessage();
    response.Content = new StreamContent(imageMemoryStream);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = pdf.Filename,
        DispositionType = "attachment"
    };
    return response;
}

Through debugging I have verified that the PdfToImages method is working and that imageMemoryStream gets filled with data from the line

pdfPagesAsImages.First().Save(imageMemoryStream, ImageFormat.Png);

However in running it, I receive an attachment that is properly named but is 0 bytes. What do I need to change in order to receive the whole file? I think it's something simple but I'm not sure what. Thanks in advance.

like image 538
Scotty H Avatar asked Oct 02 '15 15:10

Scotty H


People also ask

What is C in IP route?

Like IPv4, a 'C' next to a route indicates that this is a directly connected network. An 'L' indicates the local route. In an IPv6 network, the local route has a /128 prefix. Local routes are used by the routing table to efficiently process packets with a destination address of the interface of the router.

Why is OSPF not showing in routing table?

If an external route has an external forwarding address, the route will not be placed in the routing table. This is done to prevent routing loops. Perform a 'sh ip ospf database external" on the network address in question. You will see the forwarding address.


1 Answers

After writing to the MemoryStream, Flush it then set Position to 0:

imageMemoryStream.Flush();
imageMemoryStream.Position = 0;
like image 107
Matteo Umili Avatar answered Oct 03 '22 08:10

Matteo Umili