DelphiFAQ Home Search:
General :: Web publishing
Information about web publishing, how to maintain, optimize and promote a web site.


This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

Original file name in a http file upload form


I need to provide a file upload form on my web site and want to preserve the original file name.
Right now the uploaded files arrive on my web server in the temp folder and have file names like

How can I obtain the original file name?


The http upload protocol does not capture the local file name on the client side. What you can do is to write some javascript function that hooks into the file upload field's onchange() event.
There take the field's value (which will be the remote file name) and copy it into a hidden input field.
The hidden input field will be passed as part of your form.
You CGI script then can evaluate it and maybe do something useful with it.

Possible values for the remote file would be:

"c:\documents and settings\photo.jpg"

and others, depending on the remote operating system. Your CGI script needs to be prepared for all these possibilities and - in the examples - it probably should extract 'photo.jpg' as the file name.

The code below looks for the \ and for the / as path delimiters.

Make sure to set the form's encoding attribute with enctype="multipart/form-data"

<form enctype="multipart/form-data" action="" method=post>
 <script type="text/javascript" language="JavaScript">
 function file_onchange() {
   var s=document.getElementById('userfile').value;
   var r=s.lastIndexOf('/');
   if (r<0) {
   if (r>=0) {
 <input type=file name=userfile id=userfile onChange="file_onchange()">
 <input type=hidden name=remotename id=remotename>
 <input type=submit value="send file">

Generated 8:02:52 on Aug 19, 2017