简体   繁体   中英

File upload not not working on upload button

I am trying to upload file on server using jquery and web handler. All works fine but issue is when i click on file upload control it pops up a file selection window, after i select file it directly started file uploading. My issue is i want upload file on different control's click event(ie On button click event).

Here is my code which i am currently working.

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

I want my upload process should start on btnSub click event. If anyone knows than please help me.........

you can use jquery uploadify

Link for sample code -- download

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM