How to get content of selected file by jQuery + Ajax

Go To


I have a file upload element as hereafter:

<input type="file" id="uploadFile">

I can get the path & name of selected file by jQuery script as hereafter:

var filePath = $("#uploadFile").val();
var fileName = filePath.split('\\').pop();

Could you please advise me how to get the file content (i thought it is in object or string format) and send it to server action.

2012-04-04 07:23
by user752433
What kind of file, are you trying to load? And what is the backend - Starx 2012-04-04 07:27


First of all, you cannot get the path of an file-input element using $("#uploadFile").val();. It is a restriction by most of the modern browsers for security reasons.

Next, you have to upload the first, read the contents and throw the content back to the page using ajax.

You can use jQuery Form plugin to submit the form and grab the file contents on your backend.

2012-04-04 07:25
by Starx