首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过WebApi调用从页面下载excel文件

通过WebApi调用从页面下载excel文件
EN

Stack Overflow用户
提问于 2013-02-12 19:48:26
回答 6查看 56.5K关注 0票数 20

我正在尝试发送一个9MB的.xls文件作为web api控制器方法的响应。用户将单击页面上的按钮,这将通过浏览器触发下载。

这是我到目前为止得到的,但它不工作,但它也不会抛出任何异常。

代码语言:javascript
复制
[AcceptVerbs("GET")]
public HttpResponseMessage ExportXls()
{
    try
    {
        byte[] excelData = m_toolsService.ExportToExcelFile();

        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        var stream = new MemoryStream(excelData);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = "Data.xls"
        };
        return result;
    }
    catch (Exception ex)
    {
        m_logger.ErrorException("Exception exporting as excel file: ", ex);
        return Request.CreateResponse(HttpStatusCode.InternalServerError);
    }
}

下面是通过单击界面中的按钮来调用coffeescript/javascript jquery ajax。

代码语言:javascript
复制
$.ajax(
    url: route
    dataType: 'json'
    type: 'GET'
    success: successCallback
    error: errorCallback 
    )

现在我想起来,也许dataType是错的,不应该是json…

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14831860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档