0

这是服务器端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();
    });
});
4

2 回答 2

0

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

于 2013-11-12T07:48:50.630 回答
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>
于 2013-11-11T12:06:20.510 回答