From 6d9f519241f34d9d636c44b3a794e01d296c96df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9F=B9=E6=B7=87?= Date: Tue, 2 Aug 2016 20:56:48 +0800 Subject: [PATCH] fiexd quick-form file upload bug #47 --- .../xadmin/js/xadmin.plugin.quick-form.js | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/xadmin/static/xadmin/js/xadmin.plugin.quick-form.js b/xadmin/static/xadmin/js/xadmin.plugin.quick-form.js index 779300d53..9bfcb5130 100644 --- a/xadmin/static/xadmin/js/xadmin.plugin.quick-form.js +++ b/xadmin/static/xadmin/js/xadmin.plugin.quick-form.js @@ -90,7 +90,7 @@ }, this)) .fail($.proxy(function(xhr) { this.$mask.hide(); - alert(typeof xhr === 'string' ? xhr : xhr.responseText || xhr.statusText || 'Unknown error!'); + alert(typeof xhr === 'string' ? xhr : xhr.responseText || xhr.statusText || 'Unknown error!'); }, this)); } , save: function(newValue) { @@ -108,16 +108,31 @@ // } // }) + var $nonfile_input = this.$form.serializeArray(); + + var formData = new FormData(); + + $nonfile_input.forEach(function(field) { + formData.append(field.name, field.value) + }); + + var $file_input = this.$form.find("input[type=file]"); + $file_input.each(function (idx, file) { + formData.append($(file).attr('name'), file.files[0]); + }); + return $.ajax({ - data: [this.$form.serialize(), $.param(off_check_box)].join('&'), + data: formData, url: this.$form.attr('action'), type: "POST", dataType: 'json', + contentType: false, + processData: false, beforeSend: function(xhr, settings) { xhr.setRequestHeader("X-CSRFToken", $.getCookie('csrftoken')); } }) - }, + }, } $.fn.ajaxform = function ( option ) { @@ -167,7 +182,7 @@ if(!this.modal){ var modal = $('