2017-10-10 87 views
0

我有这个自定义Odata函数从下载pdf数据库下载pdf。我有一些问题Odata命名文件返回错误信息的

1.采用PDF文档名不名“reportname.pdf”它被命名为response.pdf

2.return reportBinary的错误信息为空

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

回答

1

尝试手动设置文件名:

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

我不知道你想做些什么错误信息是什么,但如果你的意思是设置字符串的内容,只是将其设置;)

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

考虑使用NotFound而不是Gone状态码(Gone具有非常明确的含义)。

+0

对不起。但仍然没有解决第一个问题的下载名称 – NinjaDeveloper

+0

在浏览器中检查响应标题。如果Content-Disposition具有正确的价值,那么问题根本与webapi无关。 – donMateo