Click to See Complete Forum and Search --> : Get the file name being uploaded ... ???


hYph3n
05-23-2006, 06:55 AM
I want to get the filename of the file that is being selected to be upload.

I have used the following code as the HTML form


<form action="uploadInvoicelogo.jsp?mode=cus_part_userdefinedfields&tab=0&action=upload" ENCTYPE="multipart/form-data" method="post" name=upload>
<table width="602" border="0" cellspacing="0" cellpadding="5" class="lightTable">

<tr>
<th colspan="2" nowrap>Uplaod Gif file for Logo:</th>
</tr>
<%
if(!flag){
%>
<tr>
<td colspan="2" align="center" nowrap><font color="red">Only gif, jpg, png images required for logo</font></td>
</tr>
<%
}
%>
<tr>
<td nowrap width="179">Enter Logo file (Gif only):</td>
<td><input type="file" class="formText" name="userDefinedField1" value="" size="31" ></td>
</tr>
<tr>
<td nowrap colspan="2" align="center">
<button name="B1" class="formButtonWide" onclick="return uploadLogo();">Upload</button></td>
</tr>
</table>
</form>



The file is being transfered to the server using the JSP. In which I am able in using this code and saving the file to a "Hardcoded" name ... but when i try to get the name of the file (by any means ... even i have tried to save the name in hidden field) i always get a null value ... :(

can any one help?

Khalid Ali
05-24-2006, 12:34 PM
humm..try this (if u need to do it on the client side)
add a hidden filed
<input type="hidden" name="hiddenFileName"/>
now when user clicks on upload, run a javascript call that will get the value from the userDefinedField1 and remove the path of file from it if u do not need it, and then set that value in the hidden field.
On the server side you can get it uing
String fileName = request.getParameter("hiddenFileName");
if(null!=fileName){
//do whatever u wanna do with the file name
}else{
//file name is not set properly
}

krzysieq
06-06-2006, 06:40 AM
I have a bit of a similar problem - the reverse of this, actually. I got the name of the file easily - the way Khalid wrote -, but I can't get the file itself. I'm writing a servlet, not a jsp page. Is there any way to get the actual file from the post table? I read that the getParameter(String name) method only returns Strings, which are obviously no good here. Any ideas?

Khalid Ali
06-06-2006, 01:20 PM
no get a file you will need to read the input stream and parse the stream for the file itself.
I'd say go to apache.org read up on apache commons fileupload framework (http://jakarta.apache.org/commons/fileupload/). It will solve all of ur file upload issues.

krzysieq
06-06-2006, 01:30 PM
Thanks, I'll go right away.

javazoom
06-06-2006, 01:56 PM
You could also try UploadBean API :
http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html

Hope it helps.