Search code examples
javascripthtmlajaxfile-upload

get the data of uploaded file in javascript


I want to upload a csv file and process the data inside that file. What is the best method to do so? I prefer not to use php script. I did the following steps. But this method only returns the file name instead of file path.So i didnt get the desired output.

<form id='importPfForm'>
<input type='file' name='datafile' size='20'>
<input type='button' value='IMPORT' onclick='importPortfolioFunction()'/>
</form>

function importPortfolioFunction( arg ) {
    var f = document.getElementById( 'importPfForm' );
    var fileName= f.datafile.value;   
}

So how can i get the data inside that file?


Solution

  • you can use the new HTML 5 file api to read file contents

    https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications

    but this won't work on every browser so you probably need a server side fallback.