分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

ajax使用formdata 提交excel文件表单到rails解析

发布时间:2023-09-06 02:16责任编辑:苏小强关键词:excel
 .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解析

原文地址:https://www.cnblogs.com/znsongshu/p/9740303.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved