Ramesh Srinivas
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
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
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-da ta" runat="server">
2. Use the following code if not ispostback:
Dim fileName as String
If If Page.Request.Files(0).Cont entLength > 0 Then fileName = If Page.Request.Files(0).File Name
3. Note that the input control does NOT save the file until you explicitly tell it to.
<form id="Form1" method="post" encType="multipart/form-da
2. Use the following code if not ispostback:
Dim fileName as String
If If Page.Request.Files(0).Cont
3. Note that the input control does NOT save the file until you explicitly tell it to.
Not straight forward, You will have to parse the file name
string fname = inputfile.PostedFile.FileN ame.Substr ing(inputf ile.Posted File.FileN ame.LastIn dexOf(@"\" ));
where inputfile is an object of HtmlInputFile
string fname = inputfile.PostedFile.FileN
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
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
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...
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.getElem entById('m yFileName' ).value = this.value;" />
<input type="hidden" id="myFileName" />
<input type="submit" onclick="document.getEleme ntById('my File').val ue = ''; return true;" value="Submit" />
</form>
<form method="post" target="test.html">
<input type="file" id="myFile" onchange="document.getElem
<input type="hidden" id="myFileName" />
<input type="submit" onclick="document.getEleme
</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!
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.getE lementById ("fUpload" ).value);
document.getElementById("f ileName"). value = document.getElementById("f Upload").v alue;
window.alert(document.getE lementById ("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>
<script lang=javascript>
function Submit()
{
window.alert(document.getE
document.getElementById("f
window.alert(document.getE
}
</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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
raterus,
Ah... but did you check Page.Request.Files(0).Cont entLength? :) If your form is not set to use encType="multipart/form-da ta", teh server-side results can be weird (i.e. content posted, but no files accessible).
Ah... but did you check Page.Request.Files(0).Cont
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("f ileName"). value .split("\\");
alert(array[array.length-1 ]);
document.getElementById("f ilename"). value = "";
return false;
}
</script>
<form action="andrew.htm" method="post" onSubmit="javascript:newSu bmit(); return false;">
<input type="text" name="test" value="Test">
<input type="file" name="fileName" value="">
<input type="submit" name="submit" value="Submit">
</form>
<script language="javascript">
function newSubmit()
{
var array = document.getElementById("f
alert(array[array.length-1
document.getElementById("f
return false;
}
</script>
<form action="andrew.htm" method="post" onSubmit="javascript:newSu
<input type="text" name="test" value="Test">
<input type="file" name="fileName" value="">
<input type="submit" name="submit" value="Submit">
</form>
ASKER
Thanks raterus, worked nicely!
Thank you all for your input.
regards,
KS
Thank you all for your input.
regards,
KS
Perhaps someone else can help you with the exact code to do this.