We help IT Professionals succeed at work.

Pass file from input type "File" to another page using Javascript

urir10 asked
Hi All

I have a page that has some input fields and a file upload field (html Input Type="File", not asp FileUpload)
When the button is submitted it passes all the values to another page that writes it to the DB.

The only problem is to upload the file, how can i pass the file to the other page?
Watch Question

you can get the value using below code
var FilePath;
FilePath = document.getElementById("FileUploadControlId").value


wouldnt that just give me the path thou?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

What do you mean "pass the file to the other page"?
The file upload needs a separate button, and should be done first.  Then the submit button submits the form data to ASP to go into the database.  As long as you do not need the FILE ITSELF in the database, which could make it grow to unmanageable proportions, all you need is the javascript to copy the file name from the file upload dialog into the FORM data as a filename --


Since you have C# in your tags, it is probably ASP.Net (which has its own zone: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/), and not Classic ASP.

> " (html Input Type="File", not asp FileUpload)"

Is there any difference in the HTML? Browsers can only do a file-upload with <input type="file" />, it does not matter how you create that on the server.

In order to have the browser upload a file, the form needs to have enctype="multipart/form-data", otherwise no file is uploaded. That creates a totally different request to the server, but in asp.net makes it easy for you, and you can retrieve the files from Request.Files