Link to home
Start Free TrialLog in
Avatar of Ramesh Srinivas
Ramesh SrinivasFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Just get file name

Hi,

Is there a way that I can use the file input type just to retrieve the name of a file?

I do not want to upload the file or post it in anyway - i just need the file name.


thanks,

KS
Avatar of raterus
raterus
Flag of United States of America image

The logic behind this would be to use the <input type="file" />, let them select the file, then, before submitting the page, using javascript, you will need to take the filename and put it into a hidden field which will be submitted.  Also, you will need to clear the value in the <input type="file"> control, so it isn't uploaded.

Perhaps someone else can help you with the exact code to do this.
1. You have to change the HTML form to accept uploaded files... I always forget this, and it WILL frustrate you troubleshooting it otherwise:
<form id="Form1" method="post" encType="multipart/form-data" runat="server">

2. Use the following code if not ispostback:

Dim fileName as String
If If Page.Request.Files(0).ContentLength > 0 Then fileName = If Page.Request.Files(0).FileName

3. Note that the input control does NOT save the file until you explicitly tell it to.
Avatar of dharmesh_amity
dharmesh_amity

Not straight forward, You will have to parse the file name

string fname = inputfile.PostedFile.FileName.Substring(inputfile.PostedFile.FileName.LastIndexOf(@"\"));

where inputfile is an object of HtmlInputFile
You might consider using a text box, though.... if you use the input tag, it does transfer the whole file. So... if you're getting the file name for a 200Mb file, the file will be transferred, instead of only the few bytes required for the file name.
Sorry I misunderstood the question
I believe the author does NOT want to upload the file, then parse the filename, that would defeat the purpose of this question...
If you will use input file, the file is always tranferred from client to server to a temporary location. So if you want to get only the file name and not want the file to be transferred then you might not be in luck
Avatar of Ramesh Srinivas

ASKER

Correct raterus...

I do NOT want to upload the file at all. All I want to do is get the filename. (besides the file sizes are 100mb plus and dot net crashes with these).

Parsing the filename to extract just the filename itself is not a problem. My problem is not having the file posted.

Raterus, I will try your method and get back to you.

thanks all,

KS
Sorry, raterus... I only read the first half-sentence of your post...

Working on the code...
Something like this should work...just get the value of the hidden field in asp.net

<form method="post" target="test.html">
  <input type="file" id="myFile" onchange="document.getElementById('myFileName').value = this.value;" />
  <input type="hidden" id="myFileName" />
  <input type="submit" onclick="document.getElementById('myFile').value = ''; return true;" value="Submit" />
</form>
Hi saleek,

 The best method is Javascript, call a JS function get the file name as it will be the last thing so you could split to an array and then use the last item in the array as this will be your filename, then you could set the input type="file" to nothing before doing a document.forms.submit

Cheers!
This code works... but still posts the file.  I suspest raterus' still does too ;)

<script lang=javascript>
      function Submit()
      {
            window.alert(document.getElementById("fUpload").value);
            document.getElementById("fileName").value = document.getElementById("fUpload").value;
            window.alert(document.getElementById("fileName").value);
      }
      </script>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <input type='file' height='20px' id='fUpload' name='fUpload' runat='server'>
                  <INPUT type="submit" value="Submit" onclick="Submit()">
                  <INPUT id="fileName" type="hidden">
            </form>
      </body>
ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
raterus,

Ah... but did you check Page.Request.Files(0).ContentLength? :) If your form is not set to use encType="multipart/form-data", teh server-side results can be weird (i.e. content posted, but no files accessible).
Sure did, throws an error, because there is no file posted!
This will get you the filename client side.

<script language="javascript">
      function newSubmit()
      {
            var array = document.getElementById("fileName").value .split("\\");
            alert(array[array.length-1]);
            document.getElementById("filename").value = "";
            return false;
      }
</script>

<form action="andrew.htm" method="post" onSubmit="javascript:newSubmit(); return false;">
      <input type="text" name="test" value="Test">
      <input type="file" name="fileName" value="">
      <input type="submit" name="submit" value="Submit">
</form>
Thanks raterus, worked nicely!

Thank you all for your input.

regards,

KS