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

时间:2013-11-11 11:00:03

标签: c# javascript jquery asp.net file-upload

这是服务器端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中运行良好,但在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();
    });
});

2 个答案:

答案 0 :(得分:0)

我有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>

答案 1 :(得分:0)

如果无法解决此问题,请尝试使用http://www.uploadify.com/