如何从字节数组返回pdf

时间:2018-07-17 14:05:49

标签: c# asp.net-web-api

我想从数组字节中获取pdf文件

[HttpGet("DownloadFile")]
public async Task<HttpResponseMessage> GetPdf()
{
    fileByte = await _context.CvFiles.Where(m => m.IdCandidat == 32 && m.Id == 37).Select(m => m.FileContent).FirstAsync();
   var fileName = await _context.CvFiles.Where(m => m.IdCandidat == 32 && m.Id == 37).Select(m => m.NameFile).FirstAsync();

    HttpRequestMessage request = new HttpRequestMessage();
    HttpResponseMessage objResponse = request.CreateResponse(HttpStatusCode.OK);
    objResponse.Content = new ByteArrayContent(fileByte);
    objResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    objResponse.Content.Headers.ContentDisposition.FileName = fileName;
    objResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

    return objResponse;
}

调用此方法时,我根本没有得到pdf。这是我收到的回复:

{"version":{"major":1,"minor":1,"build":-1,"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":{"headers":[{"key":"Content-Disposition","value":["attachment; filename=CV-Marzouk.pdf"]},{"key":"Content-Type","value":["application/pdf"]}]},"statusCode":200,"reasonPhrase":"OK","headers":[],"requestMessage":{"version":{"major":1,"minor":1,"build":-1,"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":null,"method":{"method":"GET"},"requestUri":null,"headers":[],"properties":{}},"isSuccessStatusCode":true}

为什么我没有将pdf作为要求的一部分返回?该如何解决?

0 个答案:

没有答案