Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用closedxml从web api下载excel文件_Javascript_Excel_Asp.net Web Api_Download_Closedxml - Fatal编程技术网

Javascript 使用closedxml从web api下载excel文件

Javascript 使用closedxml从web api下载excel文件,javascript,excel,asp.net-web-api,download,closedxml,Javascript,Excel,Asp.net Web Api,Download,Closedxml,我无法下载closedxml通过web API创建的excel文件。 如果我将文件保存在服务器上,看起来不错,但一旦我将其放入流并返回到web api,浏览器中只接收到损坏的文件 正如我在几篇文章中建议的那样,我使用httpResponseMessage,但在浏览器中,标题中的文件名永远不会到达 我们正在使用: “Microsoft.AspNet.WebApi”version=“5.2.3”targetFramework=“net461 “ClosedXML”version=“0.88.0”ta

我无法下载closedxml通过web API创建的excel文件。 如果我将文件保存在服务器上,看起来不错,但一旦我将其放入流并返回到web api,浏览器中只接收到损坏的文件

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

我们正在使用:

“Microsoft.AspNet.WebApi”version=“5.2.3”targetFramework=“net461

“ClosedXML”version=“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)
}
成功功能:

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

                }

请查看位于的Mvc扩展包


PS:有人告诉我,我每次都必须否认这一点。我是ClosedXML和ClosedXML.Extensions.Mvc的维护者。

请查看


PS:有人告诉我,我每次都必须否认这一点。我是ClosedXML和ClosedXML.Extensions.Mvc的维护者。

我现在可以成功下载带有以下代码的工作簿:

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);
        }
    }
}
使用ClosedXML.Excel;
使用System.IO;
Net系统;
使用System.Net.Http;
使用System.Net.Http.Header;
使用系统线程;
使用System.Threading.Tasks;
使用System.Web.Http;
命名空间ClosedXML.Extensions.WebApi.Controllers
{
公共类值控制器:ApiController
{
公共IHttpActionResult Get(int id)
{
返回新的TestFileActionResult(id);
}
}
公共类TestFileActionResult:IHttpActionResult
{
公共TestFileActionResult(int-fileId)
{
this.FileId=FileId;
}
public int FileId{get;private set;}
公共任务执行同步(CancellationToken CancellationToken)
{
HttpResponseMessage响应=null;
var ms=新内存流();
使用(var wb=new xl工作簿())
{
var ws=wb.AddWorksheet(“表1”);
ws.FirstCell().Value=this.FileId;
wb.SaveAs(ms);
Seek女士(0,SeekOrigin.Begin);
响应=新的HttpResponseMessage(HttpStatusCode.OK);
响应.内容=新的流内容(ms);
response.Content.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“附件”);
response.Content.Headers.ContentDisposition.FileName=“test.xlsx”;
response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“application/vnd.openxmlformats of cedocument.spreadsheetml.sheet”);
response.Content.Headers.ContentLength=毫秒长度;
Seek女士(0,SeekOrigin.Begin);
}
返回Task.FromResult(响应);
}
}
}

我现在可以成功下载带有此代码的工作簿:

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);
        }
    }
}
使用ClosedXML.Excel;
使用System.IO;
Net系统;
使用System.Net.Http;
使用System.Net.Http.Header;
使用系统线程;
使用System.Threading.Tasks;
使用System.Web.Http;
命名空间ClosedXML.Extensions.WebApi.Controllers
{
公共类值控制器:ApiController
{
公共IHttpActionResult Get(int id)
{
返回新的TestFileActionResult(id);
}
}
公共类TestFileActionResult:IHttpActionResult
{
公共TestFileActionResult(int-fileId)
{
this.FileId=FileId;
}
public int FileId{get;private set;}
公共任务执行同步(CancellationToken CancellationToken)
{
HttpResponseMessage响应=null;
var ms=新内存流();
使用(var wb=new xl工作簿())
{
var ws=wb.AddWorksheet(“表1”);
ws.FirstCell().Value=this.FileId;
wb.SaveAs(ms);
Seek女士(0,SeekOrigin.Begin);
响应=新的HttpResponseMessage(HttpStatusCode.OK);
响应.内容=新的流内容(ms);
response.Content.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“附件”);
response.Content.Headers.ContentDisposition.FileName=“test.xlsx”;
response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“application/vnd.openxmlformats of cedocument.spreadsheetml.sheet”);
response.Content.Headers.ContentLength=毫秒长度;
Seek女士(0,SeekOrigin.Begin);
}
返回Task.FromResult(响应);
}
}
}

问题似乎是web api调用的响应类型必须是{responseType:'arraybuffer'},而不是{responseType:'application/octet stream'}

context.$http.post('api-url',excel\u列表,
{responseType:'arraybuffer'})
.那么(
成功与功能,
错误(U函数)
}


无论如何,感谢您的快速帮助

问题似乎是web api调用的响应类型必须是{responseType:'arraybuffer'},而不是{responseType:'application/octet stream'}

context.$http.post('api-url',excel\u列表,
{responseType:'arraybuffer'})
.那么(
成功与功能,
错误(U函数)
}


无论如何,感谢您的快速帮助

不幸的是,这也不起作用。我们使用的是Web API,而不是MVC。我在javascript中收到了相同的结果,还有“FileStreamResult”“从那个分机。好的。我将监视这个问题,如果有解决方案,我将把它添加到扩展中。不幸的是,这也不起作用。我们使用的是Web API,而不是MVC。我在javascript中收到了相同的结果,还有来自该扩展名的“FileStreamResult”。我会监视这个问题,如果有解决方案,我会将其添加到扩展名中。如果保存该文件,请将其重命名为.zip,可以打开吗?我不是