使用jQuery获取文件输入字段

I have a form with two file upload fields. When you handle files uploaded through HTML with PHP normally, you get a $_FILES array with a bunch of entries like $_FILES['name'], $_FILES['tmp_name'], etc. that contain all the file information.

I'd like to be able to get all of that information with jQuery. Simply doing $('[name="textfile1"]').val() just gives me C:/fakepath/test.txt if I upload test.txt. Is there a way to get the actual array of information? I have a bunch of text input fields along with the file upload fields and I want to be able to pass all the information at once with an AJAX call.

    <form action="" method="post" enctype="multipart/form-data">
        <div class="input">
            <p>Upload the sitelist (.txt files only):</p>
            <input style="margin-top:0;margin-bottom:6px" type="file" name="textfile1" /><br />         
        </div>

        <div class="input">
            <p>Upload the dictionary (.txt files only):</p>
            <input style="margin-top:0;margin-bottom:6px" type="file" name="textfile2" /><br />         
        </div>
    </form> 

I hate to rain on your parade, but that is not possible.

PHP processes things server-side. The information generated when you submit the form (such as file size, file type, file tmp paths, etc.) is all generated on form submit. If this was possible, form validation would be much easier, as it could check during the insertion of the file. However, that would be a security risk too, as it would expose the current username (by the file paths containing usernames).

You mentioned that you wanted to push it via ajax. Files can't be sent asynchronously. This can be done by simply submitting the form via ajax. However, you won't be able to get the file information without first going through PHP & generating the variables server-side.

If you simply want the file name & other generic information (not the paths that PHP generates), take a look at the HTML5 File APIs at HTML5Rocks' Tutorial for File APIs here.

I hope I helped you. ;)