HTML:
<form id="formLogo">
<input type="file" id="logo" name="logo" accept="image/x-png, image/gif, image/jpeg" />
</form>
的javascript:
var myForm = new FormData();
myForm .append("file", $("#logo").files[0]);
myForm .append("type", "company");
myForm .append("accountId", "3124234");
$.ajax({
url: '/efrcetrdmbo/app/file/upload',
data: myFormData,
dataType: 'text',
processData: false,
contentType: false,
type: 'POST',
success: function (data) {
alert("sucess");
}
});
IE9不支持FormData。我想用iFrame上传文件和其他一些数据。
答案 0 :(得分:0)
与其在html中添加iframe,还不如在上传和删除之前创建iframe。
提供正确的url,文件即被上传。
JS:
function fileUpload(form, action_url) {
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "frame");
iframe.setAttribute("name", "frame");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("frameborder", "0");
form.parentNode.appendChild(iframe);
iframeId = document.getElementById("frame");
var eventHandler = function () {
if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
else iframeId.removeEventListener("load", eventHandler, false);
if (iframeId.contentDocument) {
content = iframeId.contentDocument.body.innerHTML;
} else if (iframeId.contentWindow) {
content = iframeId.contentWindow.document.body.innerHTML;
} else if (iframeId.document) {
content = iframeId.document.body.innerHTML;
}
setTimeout('iframeId.parentNode.removeChild(iframeId)', 200);
}
if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);
form.setAttribute("target", "frame");
form.setAttribute("action", action_url);
form.setAttribute("method", "post");
form.submit();
}
HTML:
<form>
<input type="file" name="file" /></br>
<input type="button" value="upload" onClick="fileUpload(this.form,'url'); return false;" >
</form>