文件上传代码适用于Chrome和Mozilla,但不适用于IE 8,9

File upload code works in Chrome and Mozilla but not in ie 8,9

本文关键字:适用于 不适用 IE 代码 Chrome 文件 Mozilla      更新时间:2023-09-26

这是服务器端的 c# 代码

 protected void btnUpload_Click1(object sender, EventArgs e)
    {
        HttpPostedFile file = Request.Files["btnFileUpload"];
        if (file != null && file.ContentLength > 0)
        {
            string fname = Path.GetFileName(file.FileName);
            file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
        }
    }

这段代码在Chrome和Mozilla中运行良好,但在ie 8,9中Request.Files["btnFileUpload"]为空。

这是 html ...

 <form id="form1" runat="server" enctype="multipart/form-data">
<div class="fileName">
</div>
<div id="plus" class="uploadPlusBtn">
</div>
<input type="file" id="btnFileUpload" runat="server" />
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click1" Text="Upload" />
</form>

以及添加的jQuery

  $(function () {
    var wrapper = $('<div/>').css({ height: 0, width: 0, 'overflow': 'hidden' });
    var fileInput = $('#btnFileUpload').wrap(wrapper);
    $('#plus').click(function () {
        fileInput.click();
    });
});

我有IE 8,你的代码对我来说工作正常,Request.Files["btnFileUpload"]返回HttpPostedFile。这是.aspx代码:

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        $(function () {
            var wrapper = $('<div/>').css({ height: 0, width: 0, 'overflow': 'hidden' });
            var fileInput = $('#btnFileUpload').wrap(wrapper);
            $('#plus').click(function () {
                fileInput.click();
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <div class="fileName">
    </div>
    <div id="plus" class="uploadPlusBtn">
    </div>
    <input type="file" id="btnFileUpload" runat="server" />
    <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click1" Text="Upload" />
    </form>
</body>
</html>

如果你不能解决这个问题,试着使用 http://www.uploadify.com/