Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't get Text value from ENCTYPE=multipart/form-data

 
David Donovan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple html page for uploading a file. The whole file uploading thing is working fine , but would you believe I cannot figure out how to get the actual text from the given code below , in my servlet.
<FORM METHOD="POST" ACTION="/servlets/upload" ENCTYPE="multipart/form-data">
<INPUT TYPE="FILE" NAME="FILE1" SIZE="50">
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>
So lets say a user clicks the "Browse" button and locates the file for upload. It's: c:\files\somefile.txt
I want to , in my servlet (doPost) grab this information. I want to have c:\files\somefile.txt assigned to a String so I can use it from there.
I'm sure this is simple , just having a braincramp here.
Thanks for all you help in advance.
David
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm
1o) So you only want to upload a .txt file, right?
2o) Are you using any package like JSPSmartUpload or Jason Hunter's oreilly package?
3o) One of the ways that pop up to my mind:
- Upload the file to any location in your server
- Read the file from *your* server and do the processing you're required to do.
HTH
 
David Donovan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply however I'm not sure my question was clear enough.
I dont want to do ANY processing ( uploading or deleting ) , I just want to get that text value from the <INPUT TYPE=FILE NAME="FILE1"> text box.
I have all the other processing in place , just want to capture the text value of what is in that box upon POSTing to the servlet.
It could be any old extension , doesnt matter. I just need to get the name of the file that is being uploaded , and place it in a String.
Thanks
[ September 14, 2003: Message edited by: David Donovan ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh I see..
I've used JSPSmartUpload, and the way I do it is:

Since I still don't know which API you're using to upload (or do your processing), I can't help you in retrieving the file name.
As you can see it's pretty straightforward.
Hope it helps
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, if you're using the oreilly classes, here's how you do it.
 
David Donovan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies. I now see how to do it using JSPsmartupload , which I am using ....
BUT , isn't there a simple way to get a name from this textbox"
<INPUT TYPE=FILE NAME=FILE1>
??
Put all upload and download processing aside. There must be a way to simply capture that text value for FILE1 ?
Thanks agaain,
David
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea . If you find the way please let me know. Probably some javascript??
good luck.
[ September 14, 2003: Message edited by: Andres Gonzalez ]
 
himanshu patel
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Hope this javascript code will help you.
<HTML>
<HEAD>
<SCRIPT LANGUAGE=javascript>
<!--
function setFileName(strFilePath){
alert(strFilePath);
strFileName = strFilePath.substring(strFilePath.lastIndexOf('\\') + 1, strFilePath.length);
alert(strFileName);
document.forms[0].hiddenFileName.value = strFilePath
alert(document.forms[0].hiddenFileName.value);
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1><P> </P>
Upload File:<INPUT type="file" name=file1 onChange="javascript:setFileName(this.value)"><p>
<INPUT type="hidden" name=hiddenFileName>
</FORM>
</BODY>
</HTML>

You can get the name of file/file path from hidden parameter in your servlet.
HTH
 
David Donovan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ... works fine up until i get to my servlet ... It is seeing null when I try to access via request.getParameter("file_name"); (changed variable to file_name)
Does this have something to do with the ENCTYPE ??? Please read up for the exact ENCTYPE that I'm using.
The Javascript seems to write the hidden field perfectly , but I still cant get at it when I get to my servlet.
Thanks for all the help folks ! Greatly appreciated.
David
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Donovan:
Thanks ... works fine up until i get to my servlet ... It is seeing null when I try to access via request.getParameter("file_name"); (changed variable to file_name)
Does this have something to do with the ENCTYPE ??? Please read up for the exact ENCTYPE that I'm using.
The Javascript seems to write the hidden field perfectly , but I still cant get at it when I get to my servlet.
Thanks for all the help folks ! Greatly appreciated.
David

Thanks for posting your results. Yup, it's got to do with the ENCTYPE. You cannot use request.getParameter("file_name"). Essentially, all the parameters that you access like that will return null, even if you have some values in it. That's the reason you use JSPSmartUpload, these classes support ENCTYPE. They have the getParameter method, getParameterNames, etc.
I guessed you were able to do it in Javascript, but still not sure how to send *that* value to the servlet.
 
David Donovan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So , using jspSmartUpload ... What would be the exact code to get the filename from within a JSP page ?
I ultimitally want to use it in : <%= strFileName %> this way ....
What exactly i'm doing , now that your all curious is starting with a file_upload.jsp page , which calls the upload.jsp page to actually upload the file. From there, upload.jsp forwards (with a refresh meta-tag) to a file_details.jsp page. It's within that particular page I want to pre-fill a disabled form field with the actual file name. This info is written to a database for use elsewhere on the site ( support downloads pages )
This make since ?
Thanks,
David
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Donovan:
So , using jspSmartUpload ... What would be the exact code to get the filename from within a JSP page ?
I ultimitally want to use it in : <%= strFileName %> this way ....
What exactly i'm doing , now that your all curious is starting with a file_upload.jsp page , which calls the upload.jsp page to actually upload the file. From there, upload.jsp forwards (with a refresh meta-tag) to a file_details.jsp page. It's within that particular page I want to pre-fill a disabled form field with the actual file name. This info is written to a database for use elsewhere on the site ( support downloads pages )
This make since ?


Nope
I don't have the API for JSPSmartUpload at the moment. If you only want to display the file name in file_details.jsp, then the way I showed you in previous post is the way to do it. It's really a piece of cake to get the name of the file.
There might be another useful methods as well. have a look at them.
- Get the file name in upload.jsp and store it in a session or request.
- forward it to the details page and retrieve it from the session/request.
does that make sense?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic