Posted content type isn't multipart/form-data

在有文件上传的表单提交过程中,搞不好就会报Posted content type isn't multipart/form-data的错误。

解决办法

<form class="form-horizontal" action="${ctx}/project/addProjectReback?callbackType=closeCurrent" 
	enctype="multipart/form-data" method="post" οnsubmit="return iframeCallback(this, dialogAjaxDone)">

<input type="file" name="image" class="required" accept="image/gif, image/jpeg, image.png">
  1. enctype="multipart/form-data"必须有这段代码。
  2. 必须对该上传文件类型的表单进行封装,区分普通的表单上传。
/**
 * 带文件上传的ajax表单提交
 * 
 * @param {Object}
 *            form
 * @param {Object}
 *            callback
 */
function iframeCallback(form, callback) {
	YUNM.debug("带文件上传处理");

	var $form = $(form), $iframe = $("#callbackframe");

	// 富文本编辑器
	$("div.editor", $form).each(
			function() {
				var $this = $(this);
				var editor = "<input type='hidden' name='" + $this.attr("name") + "' value='"
						+ $.base64.btoa($this.html()) + "' />";
				$form.append(editor);
			});

	var data = $form.data('bootstrapValidator');
	if (data) {
		if (!data.isValid()) {
			return false;
		}
	}

	if ($iframe.size() == 0) {
		$iframe = $("<iframe id='callbackframe' name='callbackframe' src='about:blank' style='display:none'></iframe>")
				.appendTo("body");
	}
	if (!form.ajax) {
		$form.append('<input type="hidden" name="ajax" value="1" />');
	}
	form.target = "callbackframe";

	_iframeResponse($iframe[0], callback || YUNM.ajaxDone);
}
function _iframeResponse(iframe, callback) {
	var $iframe = $(iframe), $document = $(document);

	$document.trigger("ajaxStart");

	$iframe.bind("load", function(event) {
		$iframe.unbind("load");
		$document.trigger("ajaxStop");

		if (iframe.src == "javascript:'%3Chtml%3E%3C/html%3E';" || // For
		// Safari
		iframe.src == "javascript:'<html></html>';") { // For FF, IE
			return;
		}

		var doc = iframe.contentDocument || iframe.document;

		// fixing Opera 9.26,10.00
		if (doc.readyState && doc.readyState != 'complete')
			return;
		// fixing Opera 9.64
		if (doc.body && doc.body.innerHTML == "false")
			return;

		var response;

		if (doc.XMLDocument) {
			// response is a xml document Internet Explorer property
			response = doc.XMLDocument;
		} else if (doc.body) {
			try {
				response = $iframe.contents().find("body").text();
				response = jQuery.parseJSON(response);
			} catch (e) { // response is html document or plain text
				response = doc.body.innerHTML;
			}
		} else {
			// response is a xml document
			response = doc;
		}

		callback(response);
	});
}

另外,关于jfinal的文件上传,请参照jfinal文件上传

笑对现实的无奈,不能后退的时候,不再傍徨的时候,永远向前 路一直都在──陈奕迅《路一直都在》
Posted content type isn't multipart/form-data

 

上一篇:项目管理实用工具包


下一篇:kafka 环境搭建