0

I have this custom Odata function to download pdf from download pdf database. I have some issues

1.with Pdf name does not name "reportname.pdf" it is named response.pdf as

2.return error message of reportBinary is null

   [HttpGet]
        [ODataRoute("GetDownloadReport(downloadId={downloadId})")]
        public HttpResponseMessage GetDownloadReport(Guid downloadId)

       var received = DateTime.UtcNow;
        byte[] reportBinary = null;
        string queryString = "SELECT report FROM downloads WHERE id = @downloadId ";
        bool success = false;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
          //get the binary from database 
        }

        HttpResponseMessage response = null;
        try
        {

            if (reportBinary == null)
                return Request.CreateResponse(HttpStatusCode.Gone);


            response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new ByteArrayContent(reportBinary);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
                FileName = "PORTName.pdf"
            };
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            return response;
        }
        catch (Exception ex)
        {

            return Request.CreateResponse(HttpStatusCode.Gone);
        }
}
NinjaDeveloper
  • 1,620
  • 3
  • 19
  • 51

1 Answers1

1

try to set filename manually:

String headerInfo = "attachment; filename=" + System.Web.HttpUtility.UrlPathEncode("PORTName.pdf");
response.Content.Headers.Add("Content-Disposition", headerInfo);

I'm not sure what do you want to do about error message, but if you mean setting string content, just set it ;)

response = Request.CreateResponse(HttpStatusCode.Gone);
response.Content = new StringContent(...);
return response;

Consider using NotFound instead of Gone status code (Gone has very specific meaning).

donMateo
  • 2,334
  • 1
  • 13
  • 14