0

我正在使用axlsx(使用axlsx_rails进行模板)。

我的控制器中有这个:

  def listado_publicaciones
    if params[:publicaciones]
      @Publicaciones = params[:publicaciones]
    else
      @Publicaciones = Proyecto.where("concurso_id","=",@concurso.id).as_json({:include => [:creador => {:only =>[:id],:include =>[:ficha =>{:only =>[:nombres,:apellidos], :methods => [:fullname],:include => [:publicacions => {:only =>[:id,:omitir,:anio,:titulo,:revista_nombre,:primer_autor,:autor_correspondiente,:coautor,:estado_id],:include => [:subtipo]}]}]}]})
    end
    respond_to do |format|
      format.html
      format.json { render :json => @Publicaciones}
      format.xlsx
    end
  end

(我稍后将使用 JBuilder 来避免冗长的@Publicaciones ...)

好吧,“listado_publicaciones”首先显示一个 html 视图,其中使用 angular 获取出版物的 json 列表,并且用户可以在生成 excel 之前丢弃一些项目。然后用户单击“To Excel”按钮调用一个angularjs服务:

@project.service 'concursos_xlsx', [
  '$resource'
  ($resource) ->
    $resource '/concursos/:id/:action.xlsx', { id: '@id' },
      listado_publicaciones:
        method: 'POST'
        params: action: 'listado_publicaciones'
] 

我使用 POST,因为我发回了一个 $scope,其中包含我第一次在 Json 上获得的出版物,这是一个非常大的出版物列表和一些其他数据,所以如果我尝试使用 GET 将无法工作,因为网址很长...

然后,同一个控制器“listado_publicaciones”获取参数publicaciones (params[:publicaciones]) 并响应格式xlsx...

然后,我的名为“listado_publicaciones.xlsx.axlsx”的模板生成了excel文件......但我不知道如何下载它......如果我检查chrome开发人员工具,我可以看到:

XHR finished loading: POST "http://localhost:3000/concursos/1/listado_publicaciones.xlsx".

如果我双击那里,我实际上可以下载 xlsx 文件......但我只是不知道如何在单击“To Excel”按钮时下载文件......我试过使用“ send_data ”, “ send_file ”,但也许我以错误的方式使用它们......请帮助,谢谢!

4

0 回答 0