Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finding mimetype using javascript

 
Rahul Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have to upload a file for that I have to pass OsType, fileXtension, mimeType and creatorCode form html to my servlet how can I derive mimetype and other parameters at client side using javascript and pass it to my servlet.

Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't much you can do with the 'file' input type from Javascript.
This is by design.
Have you looked at ServletContext.getMimeType?
as a possible way of finding the mime type once the file has been uploaded?
 
Rahul Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

My Requirement is :--

User selects the file to be uploaded and based on file's info like file mime-type,operating system type have to get properties associated with this kind of file and display these properties to user in a dialog which prompts user if he wants to upload the file. If user clicks upload button on the the file is uploaded otherwise not.
Now what should I do upload file twice once to find mime-type, operating system type and get properties of this type of file from my server and then when user clicks upload button again upload the file.
Please suggest better apporach.

Thanks
with regards
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to upload it twice. You can keep the file on the server, and in case the user decides that it is the one he wants to upload, use the file that's already there. In this case you need to periodically remove unused uploaded files from the server.
 
Rahul Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I should store uploaded file(in first call) in usersession.
and if user selects to upload file upload the file otherwise remove it from usersession.

Donot want to use class member variable for storing the file because it can cause threading issues.


Thanks going to try it.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't try to keep the entire file in memory.
Write it to disk but keep the file path and name in session.
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should find a solution which requires NO file transfer for the purpose of obtaining the file metadata (never mind TWO file transfers). If you require the user to upload a file in order to be able to decide if he wants to upload a file, not only is your architecture completely flawed but the application is useless for large files.

Effective file uploads using html forms and servlets are very difficult. Part of the problem is getting the file metadata from the client without actually downloading the file and the other part is using the metadata to drive the interaction and performing the actual download. Although javascript can probably be used to do the first part, it is very limited and poor. Applets are far more effective (check out JUpload from sourceforge) as is Flash. For the second part, check out the way the spring or tapestry frameworks handle file uploads, as well as apache commons fileupload.
 
Rahul Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you all

Problem could have been easily solved if somehow I could have been able to find files meta data at client side using javaScript.


Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Emanuel Kadziela:
You should find a solution which requires NO file transfer for the purpose of obtaining the file metadata (never mind TWO file transfers).


Adding applets or embedded flash just to determine a file's mime type, before uploading doesn't sound like a good idea to me.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic