ie9以下ajax上传文件,使用AJAX上传文件而不使用FormData(IE9)

Mouser..

8

这段代码应该可以解决问题.抱歉很久以前,我认为IE9也可以使用XHR上传(应该,但这是Iframe选项).

它执行以下操作:

向页面添加文件输入(也可以用HTML格式)

将该文件选择器放在一个表单中

向表单添加凭据

将表单提交给iframe并将其页面用作返回值.

fileSelection = document.createElement("div");

//create the file input

fileSelection.browseSelect = document.createElement("input");

fileSelection.browseSelect.type = "file";

fileSelection.browseSelect.name = "file[]";

fileSelection.browseSelect.style.display = "block";

fileSelection.browseSelect.style.position = "absolute";

fileSelection.browseSelect.style.left = "50%";

fileSelection.browseSelect.style.top = "auto";

fileSelection.browseSelect.style.height = "36px";

fileSelection.browseSelect.style.width = "36px";

fileSelection.browseSelect.style.bottom = "0px";

fileSelection.browseSelect.style.margin = "0px 0px -1px 90px";

fileSelection.browseSelect.style.filter = "alpha(opacity=0)";

fileSelection.browseSelect.style.zIndex = 14;

//Put a form in it.

fileSelection.form = document.createElement("form");

fileSelection.form.method = "POST";

fileSelection.form.action = [url to server]; //put your own file upload handler here.

fileSelection.form.enctype = "multipart/form-data";

fileSelection.form.encoding = "multipart/form-data";

fileSelection.appendChild(fileSelection.form);

//Append the file input to the form.

fileSelection.form.appendChild(fileSelection.browseSelect);

document.body.appendChild(fileSelection);

function doUploadObjectUpload()

{

var tempFrame = document.createElement("iframe");

tempFrame.src = "";

tempFrame.allowTransparancy = "true";

tempFrame.style.display = "none";

tempFrame.frameBorder = 0;

tempFrame.style.backgroundColor = "transparent";

tempFrame.onload = followUpOnHTML4Upload.bind(this,tempFrame);

tempFrame.name = "tmpFrameUpload"

this.appendChild(tempFrame);

this.form.target = tempFrame.name;

this.form.name = "uploadForm";

this.form.acceptCharset = "UTF-8"

//This is an example of a hidden input, used to pass extra vars to the server. Add more if you need them.

var tempNodePath = document.createElement("input");

tempNodePath.type = "hidden";

tempNodePath.value = [dir]; //if you want specify a target path.

tempNodePath.name = "filePath";

this.form.insertBefore(tempNodePath, this.form.childNodes[0]);

this.form.submit();

}

function followUpOnHTML4Upload(frameId)

{

//Here you can check the response that came back from the page.

}

例如,PHP将存储文件 $_FILES

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值