【问题标题】:File size validation in javascript / jQuery? [duplicate]javascript / jQuery中的文件大小验证? [复制]
【发布时间】:2013-12-01 19:23:23
【问题描述】:

我想在上传到服务器之前检查文件(图像)的大小。我正在使用 .aspx 页面。此文件上传控件在 jquery 对话框中。当用户单击确定按钮时,文件大小验证应该可以工作。

这是我到目前为止所做的代码:

<a id="lnkUploadImg" href="#">Upload Image</a>
                                <br />
                                <div id="myUploadImgDialog" title="Image Upload">
                                    <table>
                                        <tr>
                                            <td align="right">
                                                Image:
                                            </td>
                                            <td>
                                                <input type="file" name="file" value="" id="imgUpload" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right">
                                                Description:
                                            </td>
                                            <td>
                                                <asp:TextBox ID="txtUploadImgDescription" runat="server" TextMode="MultiLine" Width="285"
                                                    Height="80"></asp:TextBox>
                                            </td>
                                        </tr>
                                    </table>
                                </div>

脚本如下:

<script type="text/javascript">
    $(function () {
        $("#myUploadImgDialog").dialog({
            autoOpen: false,
            width: 475,
            modal: true,
            buttons: {
                "OK": function () {
                    if (navigator.appName == "Netscape") {
                        // Validation Code Here
                        // If valid return true else return false
                    }
                    if (navigator.appName == "Microsoft Internet Explorer") {
                        // Validation Code Here
                        // If valid return true else return false
                    }
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });
        $("#lnkUploadImg").click(function () {
            $("#myUploadImgDialog").dialog("open");
        });
    });
</script>

请帮助我。提前致谢。

【问题讨论】:

  • 试图嗅探浏览器的坏主意...改用特征检测。你是怎么上传的?

标签: javascript jquery


【解决方案1】:

使用imgUpload的change事件来获取你需要的信息。

<button type="button" onclick="javascript: document.getElementById('imageUpload').click();">Upload</button>

var filesInput = document.getElementById("imgUpload");
filesInput.addEventListener("change", function(event){
    var file = event.target.files[0];
    console.log(file.size);
});

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多