IE8发布文件的目标iframe,到达服务器为空 [英] IE8 post file targeting iframe, arrives on server as null

查看:122
本文介绍了IE8发布文件的目标iframe,到达服务器为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有 input:file 的表单,表单的目标是一个名为 iframe 的表单。当用户选择一个文件时,它会自动将表单发布到服务器上。这可以在IE10 / firefox / chrome中使用,但是在IE8中,我的控制器方法中的File参数在IE8发布表单时为null。有没有其他人遇到这种情况,并知道任何解决方案,为什么不是IE8实际发布文件数据?

I have a form with just an input:file in it and the form targets a named iframe. when the user selects a file it automatically posts the form to the server. This works in IE10/firefox/chrome, but in IE8 the File parameter on my controller method is null when IE8 posts the form. Has anyone else encountered this and know of any solutions, why isn't IE8 actually posting the file data?

ClientSide:

ClientSide:

function createFileUploadForm()
{
    var frameName = 'fileUploadFormFrame';
    var fileValue;
    var fileUploadCallback = function()
    {
        //do stuff when the server responds after receiving the file
    };
    var fileInputChangedCallback = function(event)
    {
        if(fileInput.value != fileValue)
        {
            fileValue = fileInput.value;
            form.submit();
        }
    };

    var iFrame = document.createElement('iframe');
    iFrame.name = frameName
    document.body.appendChild(iFrame);

    var form = document.createElement('form');
    form.action = 'a/valid/url';
    form.method = 'post';
    form.enctype = 'multipart/form-data';
    form.target = frameName;

    var fileInput = document.createElement('input');
    fileInput.type = 'file';
    fileInput.name = 'File';
    fileInput.accept = '.spc';
    fileValue = fileInput.value;

    //all browsers except IE8
    //add event listener to fileInput onChange event -> fileInputChangedCallback

    //IE8 fix
    //add event listener to fileInput onFocus event -> fileInputChangedCallback

    form.appendChild(fileInput);

    document.body.appendChild(form);
}

ServerSide:

ServerSide:

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase File)
{
    //do stuff with File, but in IE8 File parameter is null
}


推荐答案

问题在于IE8需要额外的 encoding 要设置的表单上的属性:

the problem was that IE8 requires an additional encoding property on the form to be set:

var form = document.createElement('form');
form.action = 'a/valid/url';
form.method = 'post';
form.enctype = 'multipart/form-data';
form.encoding = 'multipart/form-data';  //this additional line fixes the IE8 problem I was having
form.target = frameName;

这篇关于IE8发布文件的目标iframe,到达服务器为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆