ajax使用formdata 提交excel文件表单到rails解析
.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
ajax使用formdata 提交excel文件表单到rails解析的更多相关文章
- Ajax 使用formdata 实现 无刷新表单上传
FormData对象的作用就类似于这里的serialize()方法,不过FormData是浏览器原生的,且支持二进制文件 1.这里实现一个无刷新上传图片,成功后页面显示 点击button 触发隐藏的 ...
- Ajax发送FormData对象封装的表单数据
前端页面: <!doctype html> <html lang="en"> <head> <meta charset="UTF ...
- Java使用HTTP编程模拟多参数多文件表单信息的请求与处理
本文目的是提供Java环境下模拟浏览器页面提交多参数多文件表单请求以及解析请求的Demo代码.这里用Java提供的HttpURLConnection类做HTTP请求,再原始点可以直接使用socket. ...
- SpringMVC 完美解决PUT请求参数绑定问题(普通表单和文件表单)
一 解决方案 修改web.xml配置文件 将下面配置拷贝进去(在原有的web-app节点里面配置 其它配置不变) <!-- 处理PUT提交参数(只对基础表单生效) --> <filt ...
- Jquery serialize()提交多个表单数据
ajax提交多个表单数据: 先把不同的表单分别用serialize()函数,然后把序列化后的数据用+拼接提交给后台,具体例子如下 var data1 = $('#form1).serialize(); ...
- javaWeb中一个按钮提交两个表单
一个按钮提交两个表单,有时候会用到,一般会很容易想到使用 onclick="document.form1.submit();document.form2.submit();" 的方 ...
- 直接提交一个form表单后台返回一个新页面通过target属性可以放到iframe当中
问题描述: 我想提交一个form表单后台直接返回一个新页面,但是当前页面还不想被替换掉: 解决方案: 在页面中添加一个iframe将form表单的target属性设置为iframe的id这样的话返回的 ...
- Form表单 JSON Content-type解析
Form表单 JSON Content-type解析 1 表单Form概述 在Form表单中,参数一般有: action 表单提交的url method 提交方式:post get name 表单的属 ...
- HTML5新表单新功能解析
HTML5新增了很多属性功能.但是有兼容性问题,因为这些表单功能新增的.我这里做了一个简单的练习,方便参考.如果完全兼容的话,那我们写表单的时候就省了很多代码以及各种判断. <!DOCTYPE ...
随机推荐
- c++ why can't class template hide its implementation in cpp file?
类似的问题还有: why can't class template use Handle Class Pattern to hide its implementation? || why there ...
- c++ virtual 和 pure virtual的区别
参考资料: http://stackoverflow.com/questions/1306778/c-virtual-pure-virtual-explained 验证代码: #include < ...
- x264_param_t结构体解释,设置及对应函数位置
typedef struct x264_param_t { /* CPU 标志位 */ unsigned int cpu; int i_threads; /* 并行编码多帧 */ in ...
- IE6鼠标悬停Bug
当鼠标放置于某个文字链接之上,文字或文字背景改变为其他颜色或样式的效果是我们最经常见到的鼠标悬停效果.在CSS中,这个效果靠伪元素:hover来实现,只不过在文字链接中:hover被应用在了锚点元素& ...
- 【转】Monkey测试6-Monkey Test Log
Moneky Test Log 分析: 首先用一个最简单的例子分析:monkey --pct-trackball 0 --throttle 100 -v 500/*p参数: 表示指定测试的程序/*v参 ...
- weixin oauth 授权
1. 先了解下请求授权页面的构造方式: https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_u ...
- java blob
package net.qysoft; import java.io.File;import java.io.FileOutputStream;import java.sql.Blob;import ...
- SQL.Cookbook 读书笔记5 元数据查询
第五章 元数据查询 查询数据库本身信息 表结构 索引等 5.1 查询test库下的所有表信息 MYSQL SELECT * from information_schema.`TABLES` WHERE ...
- jmeter对响应结果做正则、json、xpath结果测试
上面的返回结果可用于关联取值测试 具体用法详见http://www.cnblogs.com/xinjing-jingxin/p/8554338.html http://goessner.net/art ...
- [转]Linux进程通信之POSIX消息队列
进程间的消息队列可以用这个实现,学习了下. http://blog.csdn.net/anonymalias/article/details/9799645?utm_source=tuicool&am ...