jQuery文件上传 - 验证文件大小和扩展名

时间:2014-04-07 15:37:11

标签: jquery file-upload blueimp

我使用jQuery文件上传,我的代码是:

$(function(){

        var ul = $('#upload ul');
     // Initialize the jQuery File Upload plugin
        $('#upload').fileupload({
            maxFileSize: 500,
            acceptFileTypes:  /(\.|\/)(gif|jpe?g|png)$/i,
            formData: {
                action: 'ja_upload_doc',
                cv_id: $('#cv_id').val(),
                first_name: $('#first_name').val(),
                last_name: $('#last_name').val(),
            },
            // This element will accept file drag/drop uploading
            dropZone: $('#drop'),

            // This function is called when a file is added to the queue;
            // either via the browse button, or via drag/drop:
            add: function (e, data) {

                // Automatically upload the file once it is added to the queue
                var jqXHR = data.submit().success(function (result, textStatus, jqXHR) {
                    console.log('Done');
               });
            },

            progress: function(e, data){
            },
            done:function(e, data){

                console.log(data);
            },
            fail:function(e, data){
                // Something has gone wrong!
                data.context.addClass('error');
            }

        });

    });

我包括

  • jquery.ui.widget.js
  • jquery.iframe-transport.js
  • jquery.fileupload.js
  • jquery.fileupload-ui.js
  • jquery.fileupload.css

我还需要包含其他任何内容才能使验证工作吗?或者我的代码有问题吗?

3 个答案:

答案 0 :(得分:6)

要验证文件大小,请使用:maxFileSize

maxFileSize: 500 // in byte 1024 * 1024 = 1Mb // for maximum size
minFileSize: 1   // in byte also

并验证扩展程序的使用:acceptFileTypes

acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i

了解有关此option

的更多信息

答案 1 :(得分:4)

您需要按此顺序包含jquery.fileupload-process.js和jquery.fileupload-validate.js文件才能验证。

答案 2 :(得分:-4)

      <script type = "text/javascript">
        var counter = 0;
        function AddFileUpload() {
        var div = document.createElement('DIV');
        div.innerHTML = '<input id="file' + counter + '" name = "file' + counter +
         '" type="file" />' +'<input type="text" name="textbox' + counter + '" id="textbox' + counter + '" value=""  />' +
          '<input id="Button' + counter + '" type="button" ' +
          'value="Remove" onclick = "RemoveFileUpload(this)" />';
            document.getElementById("FileUploadContainer").appendChild(div);
            counter++;
        }
    function RemoveFileUpload(div) {
            document.getElementById("FileUploadContainer").removeChild(div.parentNode);
    }
    </script>

 html codes ////////////
 <div>
    <asp:Label ID="lblError" runat="server" Font-Bold="true" ForeColor="Red"> </asp:Label><br />
  <span style="font-family:Arial"><span style="color: #003366">&nbsp;Click to add files</span></span>
 <input id="Button1" onclick="AddFileUpload()" 
 style="height: 27px; width: 74px;" tabindex="25" type="button" value="add" /><br /> <br /><br />
  <div id="FileUploadContainer">
  <asp:FileUpload ID="FileUpload1" runat="server" />
  <input type="text" name="textbox' + counter + '" id="textbox' + counter + '" value=""  />
  <!--FileUpload Controls will be added here -->
                                                                                          </div>                                                                                         <br />                                                                                         <br />                                                                                          <br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" onclick="btnUpload_Click" />
<br /><br />
/<TD>
    </div>