使用Jquery或JavaScript的asp:FileUpload控件上传文件

时间:2014-11-23 08:20:52

标签: javascript c# asp.net

在我的webform中,我想使用asp:FileUpload控件上传文件,

所以当我点击Asp:按钮时;应该发生FileUpload点击,OpenFileDialog应该打开。

FilePathBrowser.ascx

<script type="text/javascript" src = "../Scripts/jquery-1.2.6.js">
$(document).ready(function () {
    $(document).on('change', '#<%= fUpload.ClientID %>', function () {
        if (document.getElementById('<%= fUpload.ClientID %>').files.length === 0) {
            return;
        }
        $('#<%= btnText.ClientID %>').trigger('click');
    });
});
</script>


    <table width="100%">
    <tr>
    <td style="width:80%;"><asp:TextBox ID="txtFilePath" runat="server" CssClass="FlatTextbox" Width="100%" ReadOnly="true" ></asp:TextBox></td>
    <td style="width:10%; padding-left:15px"><asp:Button ID="btnText" runat="server" Text="" CssClass="FlatButton" Width="100%" onclick="btnText_Click" /></td>
    <td style="width:10%"><asp:FileUpload ID="fUpload" runat="server" Visible="false" OnClick="DoOpenFileDialog()"/></td>
    </tr>
    </table>

FilePathBrowser.ascx.cs

    public partial class FilePathBrowser : System.Web.UI.UserControl
    {
        public event EventHandler BrowseClick;

        public string FilePathText 
        {
            get { return txtFilePath.Text; }
            set { txtFilePath.Text = value; }
        }

        public string ButtonText
        {
            get { return btnText.Text; }
            set { btnText.Text = value; }
        }

        public FileUpload HiddenFileUploadCtrl
        {
            get { return this.fUpload; }
            set { fUpload = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnText_Click(object sender, EventArgs e)
        {
            BrowseClick(sender, e);
        }
    }

在网页Menu.aspx

    <UserControl:FPBrowser ID = "fpbImageUpload" runat = "server" ButtonText="Browse.." FilePathText="" OnBrowseClick="fpbImageUpload_BrowseClick"/>

Menu.aspx.cs

    protected void fpbImageUpload_BrowseClick(object sender, EventArgs e)
    {
        if (fpbImageUpload.HiddenFileUploadCtrl.HasFile)
        {
            String fileName = fpbImageUpload.HiddenFileUploadCtrl.FileName;
            fpbImageUpload.FilePathText = Path.Combine(Helper.FileUploadPath, fileName);
            fpbImageUpload.HiddenFileUploadCtrl.SaveAs(Path.Combine(Helper.FileUploadPath, fileName));
        }
    }

因此对话框没有打开。有没有人知道我在代码中缺少什么。??

1 个答案:

答案 0 :(得分:1)

将此javascript函数添加到usercontrol

function hookFileClick() {
    // Initiate the File Upload Click Event
    document.getElementById('<%=fUpload.ClientID%>').click();
}

function callme(oFile) {
    document.getElementById('<%=txtFilePath.ClientID%>').value = oFile.value;
}

用户Control.ascx

    <table width="100%">
    <tr>
    <td style="width:70%;"><input ID="txtFilePath" runat="server" type="text" class="FlatTextbox" style= "width:100%;" /></td>
    <td style="width:29%; padding-left:15px"><asp:Button ID="btnText" runat="server" Text="" CssClass="FlatButton" Width="100%" OnClick="btnText_Click" OnClientClick="javascript:return hookFileClick();" /></td>
    <td style="width:1%"><asp:FileUpload ID="fUpload" runat="server" Style="visibility: hidden;width:1px" onchange="callme(this)"/></td>
    </tr>
    </table>

Menu.aspx.cs文件中的修改代码

 protected void fpbImageUpload_BrowseClick(object sender, EventArgs e)
    {
        if (fpbImageUpload.HiddenFileUploadCtrl.HasFile)
        {
            String fileName = fpbImageUpload.HiddenFileUploadCtrl.FileName;
            //fpbImageUpload.FilePathText = Path.Combine(Helper.FileUploadPath, fileName);
            fpbImageUpload.HiddenFileUploadCtrl.SaveAs(Path.Combine(Helper.FileUploadPath, fileName));
        }
    }