You can look at the struts example struts-upload.war (I think that is the
name in the examples directory) for how to do this. Basically you want to
make your form something like this:
<html:form action="uploadData.do " enctype="multipart/form-data">
<html:file property="filename" />
</html:form>
You will of course need an actionForm and an action class.
The action form would have code such as....
Public class uploadDataForm extends ActionForm {
protected FormFile filename;
public FormFile getFilename() {
return filename;
}
public setFilename(FormFile filename) {
this.filename = filename;
}
}
and in the action class....
public class UploadDataAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
UploadDataForm dataForm = (UploadDataForm) form;
FormFile file = dataForm.getFilename();
String filename = file.getFileName();
try {
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
baos.write(buffer, 0, bytesRead);
}
//close the stream
stream.close();
// You now have the file in a ByteArrayOutputStream and you can do
// something with it
}
catch (FileNotFoundException fnfe) {
return null;
}
catch (IOException ioe) {
return null;
}
}
HTH,
Al
-----Original Message-----
From: Caroline Jen [mailto:[EMAIL PROTECTED]
Sent: Monday, October 04, 2004 4:51 PM
To: [EMAIL PROTECTED]
Subject: How To Upload Files from Clients' Machine?
In my JSP I have this:
code:
-------------------------------------------------------
<input type="file" name="filename">
-------------------------------------------------------
for visitors of the web page to browse their PCs'
directories to select a file to upload (when the
Submit button is clicked). The files can be .doc,
.txt, .pdf, .jpg, ..., etc. However, I do not have to
convert files into certain format. I simply retrieve
them from clients' machine and store them into a
database. Later on, clients can ask to view those
files in their original format.
What appears in the text field:
<input type="file" name="filename">
will be a file name. How do I get both the file name
and the content of that file to be saved in the
database?
_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]