【问题标题】:download doc file in MVC在 MVC 中下载 doc 文件
【发布时间】: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


【解决方案1】:

我可以在 web api 上解决这个问题,我将字节设置为字符串,如下所示

  String doc = Convert.ToBase64String(customermandate.Content);

对于 MVC 端,我从字符串转换回字节

 var doc = restClient.Execute(request);
             var response = doc.Content.Substring(1, doc.Content.Length - 2).Replace(@"\/", "/");
  byte[] docBytes = Convert.FromBase64String(response);
             if (doc.Content != null && doc.Content.Length > 0 && !string.IsNullOrEmpty(doc.Content))
             {
                 Response.Clear();    
                 Response.ContentType = "application/pdf";
                 Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
                 Response.BinaryWrite(docBytes);    
                 Response.End();
          }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    相关资源
    最近更新 更多