2017-08-24 164 views
0

我无法使其通过web API下载由closedxml创建的excel文件。 如果我将文件保存在服务器上,它看起来不错,但只要我把它放入一个流中并将它返回给web api,那么在浏览器中只会收到一个损坏的文件。使用closedxml从web api下载excel文件

正如在几篇文章中建议我使用httpResponseMessage,但也在浏览器中的头文件名永远不会到达。

我们使用:

“Microsoft.AspNet.WebApi” 版本= “5.2.3” targetFramework = “net461

”ClosedXML“ 版本=” 0.88.0" targetFramework = “net461”

的WebAPI代码:

var wb = new XLWorkbook(); 
      var ws = wb.Worksheets.Add("Parcel List"); 


      MemoryStream fs = new MemoryStream(); 
      wb.SaveAs(fs); 
      fs.Position = 0; 


      HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); 
      result.Content = new ByteArrayContent(fs.GetBuffer()); 
      result.Content.Headers.ContentLength = fs.Length; 
      result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
      { 
       FileName = "List" + "_" + DateTime.Now.ToShortDateString() + ".xlsx" 
      }; 
      result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); 

      return result; 

下面的JavaScript代码:

context.$http.post(config.get_API_URL() + 'api_call', excel_list, 
     {responseType: 'application/octet-stream'}) 
    .then(
    success_function, 
    error_function) 
} 

success_function:

function(response) { 

        var headers = response.headers; 
       var blob = new Blob([response.body], 
            {type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}, 
            ); 

       window.open(window.URL.createObjectURL(blob)); 

       } 
+0

如果保存该文件,请将其重命名为.zip,您可以打开它吗?我试图弄清楚内部是否损坏或者包装在传输中是否被破坏。 –

+0

我将工作簿保存在服务器上,并将其重命名为.zip,可以将其打开并查看结构。所以它似乎是损坏它的传输 – TwoHeadedSquirrel

+0

如果你可以打开.zip文件,那么它不是损坏它的传输。当你将它保存在服务器上时,你能检查内容长度与文件的确切长度吗? –

回答

0

这个问题似乎是为Web API调用的响应类型必须是{responseTyp的:“arraybuffer”},而不是{的responseType : '应用程序/八位字节流'}

context.$http.post('api-url', excel_list, {responseType: 'arraybuffer'}) .then( success_function, error_function) }

无论如何谢谢你快速帮助

0

看一看MVC扩展包在https://www.nuget.org/packages/ClosedXML.Extensions.Mvc/

PS:我已经告诉我要放弃这个每次。我是ClosedXML和ClosedXML.Extensions.Mvc的维护者。

+0

不幸的是,这也行不通。我们正在使用Web API而不是MVC。我在JavaScript中也收到了相同的结果,同样也使用了该扩展中的“FileStreamResult”。 – TwoHeadedSquirrel

+0

好的。我会监视这个问题,如果有解决方案,我会将其添加到该扩展。 –

0

我可以成功下载一个工作簿现在这个代码:

using ClosedXML.Excel; 
using System.IO; 
using System.Net; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Web.Http; 

namespace ClosedXML.Extensions.WebApi.Controllers 
{ 
    public class ValuesController : ApiController 
    { 
     public IHttpActionResult Get(int id) 
     { 
      return new TestFileActionResult(id); 
     } 
    } 

    public class TestFileActionResult : IHttpActionResult 
    { 
     public TestFileActionResult(int fileId) 
     { 
      this.FileId = fileId; 
     } 

     public int FileId { get; private set; } 

     public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) 
     { 
      HttpResponseMessage response = null; 

      var ms = new MemoryStream(); 
      using (var wb = new XLWorkbook()) 
      { 
       var ws = wb.AddWorksheet("Sheet1"); 
       ws.FirstCell().Value = this.FileId; 

       wb.SaveAs(ms); 

       ms.Seek(0, SeekOrigin.Begin); 

       response = new HttpResponseMessage(HttpStatusCode.OK); 
       response.Content = new StreamContent(ms); 
       response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); 
       response.Content.Headers.ContentDisposition.FileName = "test.xlsx"; 
       response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 

       response.Content.Headers.ContentLength = ms.Length; 
       ms.Seek(0, SeekOrigin.Begin); 
      } 

      return Task.FromResult(response); 
     } 
    } 
} 
+0

谢谢,但javascript部分如何查找web api调用并自行下载?有这个问题的感觉。 – TwoHeadedSquirrel

+0

我不是JavaScript专家。但请查看https://stackoverflow.com/a/24129082/179494中的问答。看起来像你尝试做的事情有不同的支持。我的建议是尝试使用直接的''标签,而不是通过AJAX调用下载文件 –