c# - 未从路由 C# 接收数据

标签 c# .net routes attachment memorystream

我试图从服务器路由返回一张图像,但我得到的是 0 字节的图像。我怀疑这与我使用 MemoryStream 的方式有关。这是我的代码:

[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;
}

通过调试,我已验证 PdfToImages 方法正在运行,并且 imageMemoryStream 已填充来自该行的数据

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

但是在运行它时,我收到了一个正确命名但为 0 字节的附件。我需要更改什么才能接收整个文件?我认为这很简单,但我不确定是什么。提前致谢。

最佳答案

写入MemoryStream后,Flush然后将Position设置为0:

imageMemoryStream.Flush();
imageMemoryStream.Position = 0;

关于c# - 未从路由 C# 接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32910651/

相关文章:

.net - 以 .cs 结尾但不以 .g.cs 结尾的字符串的正则表达式

Angular 2路线 child 与网点

python - 没有数据通过 drf-nested-routers PUT 传输

javascript - io.sockets.on 无法在 Node.js 中的路由内工作

c# - 从网络引用下载 $metadata 时出错

c# - Azure Blob 和表存储以更简单的方式重写代码

c# - 在 Devexpress 的 LookupEdit 上显示多列

c# - ItextSharp 在 pdf 中查找文本并突出显示

.net - 在 F# 中创建一个动态大小的字节数组

.net - 使用Windows窗体设计器添加用户控件