文件上传不能处理上传按钮

时间:2012-06-29 09:40:52

标签: javascript jquery asp.net jquery-plugins

我正在尝试使用jquery和web处理程序在服务器上上传文件。一切正常,但问题是当我点击文件上传控件时弹出一个文件选择窗口,我选择文件后直接启动文件上传。我的问题是我想在不同控件的点击事件上上传文件(即点按钮点击事件)。

这是我目前正在使用的代码。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-1.2.6.min.js" type="text/javascript"></script>
    <script src="jquery.ajax_upload.0.6.min.js" type="text/javascript"></script>
    <link href="default.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" language="javascript">
        $(document).ready(function () {

            var button = $('#fuAttachment'), interval;
            $.ajax_upload(button, {
                action: 'FileUploader.ashx',
                name: 'myfile',
                onSubmit: function (file, ext) {
                    if (ext == "jpg") {
                        return true;
                    }
                    else {
                        alert("Invalid File Type.");
                        return false;
                    }
                    //  this.disable();
                },
                onComplete: function (file, response) {
                    window.clearInterval(interval);
                    $('<li></li>').appendTo('.files').text(file);
                }
            });
        });
    </script>
</head>
<body>
<form>

<table border="0" width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <td class="web_dialog_title">
                    <asp:Label ID="Label2" Text="Add Attachment" runat="server"></asp:Label>
                </td>
                <td class="web_dialog_title align_right">

                </td>
            </tr>
            <tr>
                <td colspan="2">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblAtchTitle" runat="server" Text="Attachment :"></asp:Label>
                </td>
                <td>
                    <input type="file" id="fuAttachment" runat="server" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblAtchType" runat="server" Text="Type :"></asp:Label>
                </td>
                <td>
                    <select id="ddlAtchType" runat="server">
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblAttachDesc" runat="server" Text="Description :"></asp:Label>
                </td>
                <td>
                    <textarea id="txtAttachDesc" runat="server" cols="13" rows="4"></textarea>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    <table border="0" width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                            <td>
                                <asp:Button ID="btnAddAttach" runat="server" Text="Attach" />
                                <input type="submit" runat="server" name="Submit" value="Submit" id="btnSub" />
                            </td>
                            <td>
                                <input type="reset" value="Reset" id="btnAttachReset" name="Reset" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <ol class="files">
                    </ol>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

我希望我的上传过程应该从btnSub点击事件开始。如果有人知道请帮助我.........

1 个答案:

答案 0 :(得分:0)

你可以使用jquery uploadify

Link for sample code -- download