【发布时间】:2015-12-04 16:00:37
【问题描述】:
我有应用程序,它是 MVC 4 + Web Api + SQL 服务器的组合。
我正在尝试将 doc 文件下载到 MVC,但我尝试了以下步骤。
我有 Web API,我在其中编写了以下代码。当我发送 rowid 时,它的值存储在数据库中为varbinary。文件格式可以是 .doc、pdf 等任何东西……但我正在寻找第一个 doc 或 PDF 文件格式。
当我调用 Web api 时,它会创建 PDF 文件并下载它,但文件已完全损坏。
[ResponseType(typeof(MandateExceptionDO))]
[HttpGet]
[Route("api/DealingMandate/GetExceptionDoc/{rowId}")]
public HttpResponseMessage GetExceptionDoc(int rowId)
{
IDealingMandates repository = new DealingMandatesRepository();
List<MandateExceptionDO> mandateexceptiondoc =new List<MandateExceptionDO>();
mandateexceptiondoc = repository.GetExceptionDoc(rowId);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
//response.Content = new ByteArrayContent(mandateexceptiondoc[0].Content);
//response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");
//byte[] fileBytes = System.IO.File.ReadAllBytes(mandateexceptiondoc[0].Content);
response.Content = new ByteArrayContent(mandateexceptiondoc[0].Content);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "testing.pdf";
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
//return Ok(mandateexceptiondoc);
return response;
}
【问题讨论】:
-
你如何从客户端调用这个?
-
我相信你需要从 Request.CreateResponse(...) 创建 httpresponsemessage 的实例。请参阅此问题的答案。 stackoverflow.com/questions/12563576/…
-
我正在创建 RestRequest 来调用下面的 API 是代码。 Doc 在这里获取空值 var request = new RestRequest("api/DealingMandate/GetExceptionDoc/{rowId}", Method.GET); request.AddParameter("rowId", Id, ParameterType.UrlSegment); var doc = restClient.Execute
- >(request);
-
@MatthewBrubaker 我添加了代码 HttpResponseMessage response = Request.CreateResponse
(HttpStatusCode.OK,missionexceptiondoc);在 MVC 方面,我收到以下错误 {"Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize response body for content type'application/json; charset= utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"发生错误。","ExceptionMessage":"检测到自引用循环属性 'ApplicationInstance' 类型为 'ASP.glo
标签: asp.net-web-api2