ajax使用formdata 提交excel文件表单到rails解析
2024-08-30 22:44:02
.modal-body .container-fluid .row .col-md-12 1.下载模板文件 = link_to '模板文件' .row .col-md-12 = form_tag '', :id => "my-form" do .input-group %span.input-group-btn %button#fake-file-button-browse.btn.btn-default{:type => "button"} %span.glyphicon.glyphicon-file = file_field_tag :category_file, :id => "files-input-upload", :style => "display:none" %input#fake-file-input-name.form-control{:disabled => "disabled", :type => "text"}/ %span.input-group-btn %button#fake-file-button-upload.btn.btn-default{:disabled => "disabled", :type => "button"} %span.glyphicon.glyphicon-upload
使用formdata得到完整表单,将formdata作为data值传递给后台,就如同点击submit提交数据一样。注意此处的url和type对应的值不能直接写到表单里面,而应写在ajax的配置参数中
$('#fake-file-button-upload').click(function() { var form = new FormData(document.getElementById('my-form')); $.ajax({ url: "/tax_categories/get_category", type: "POST", data: form, dataType: "json", processData: false, // 不处理数据 contentType: false, //要加 success: function(data) { console.log(data); if (data.result == "error") { alert(data.result); } else { console.log("save success"); window.location = "/tax_categories" } } }); });
使用creek解析xml
require 'creek' def get_category flag = true begin file = params[:category_file] creek = Creek::Book.new file.path sheet = creek.sheets[0] sheet.rows.each do |row| puts row end rescue flag = false end if flag respond_to do |f| f.json { render :json => {:result => "success"}.to_json } end else respond_to do |f| f.json { render :json => {:result => "error"}.to_json } end end end
此处还有个比较二的问题,是发生了302重定向,刚开始controller中的代码是如下,在解析成功后想直接跳转,可是在ajax的请求下,产生的是302重定向,在浏览器中并不会显示跳转。所以采用在js中location定位到新的页面
if flag redirect_to tax_categories_url else respond_to do |f| f.json { render :json => {:result => "error"}.to_json } end end
最新文章
- 用datagrid实现完整的一个页面
- Hibernate createCriteria查询详解
- Openvswitch原理与代码分析(4):网络包的处理过程
- web工程中web.xml元素加载顺序以及配置实例
- handler.post 为什么要将thread对象post到handler中执行呢?
- 《C++ Primer Plus 第六版》读书笔记
- Content Negotiation(内容协商)
- 201521123110《Java程序与设计》第13周学习总结
- 自增ID算法snowflake(雪花)
- [mysql] MySQL解压缩安装步骤
- mysql备份与还原 数据库的常用命令。
- react 之 reflux 填坑
- [leetcode]44. Wildcard Matching万能符匹配
- c#中的模态对话框和非模态对话框
- AngularJS 杂项知识点
- 提供PPT嵌入Winform/WPF解决方案,Winform/WPF 中嵌入 office ppt 解决方案
- C中预编译详解
- docker 创建jdk镜像
- 概率论与数理统计基础<;1>;:随机事件与随机变量
- T-SQL with as 的用法(转) SQL 下的递归查询 SQL2005(CTE) ,SQL2000(Function 递归)
热门文章
- iOS开发多线程篇 11 —自定义NSOperation
- python requests.exceptions.ConnectionError
- audio的总结
- Unity3D学习笔记——NGUI之UIScrollBar
- android自定义View_3——Optimizing the View
- 光圈与F数相关知识
- C和MATLAB中:同时对多个变量连续赋值
- JavaScript HTML DOM增删改查
- 47、ListView setSelection() 和 setSelectionFromTop()
- linux压缩、解压缩命令