aspose file tools*
The moose likes Servlets and the fly likes finding mimetype using javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "finding mimetype using javascript" Watch "finding mimetype using javascript" New topic
Author

finding mimetype using javascript

Rahul Sharma
Greenhorn

Joined: Apr 01, 2007
Posts: 7
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

Joined: Dec 11, 2004
Posts: 13410

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?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rahul Sharma
Greenhorn

Joined: Apr 01, 2007
Posts: 7
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
Marshal

Joined: Mar 22, 2005
Posts: 41577
    
  54
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.


Ping & DNS - my free Android networking tools app
Rahul Sharma
Greenhorn

Joined: Apr 01, 2007
Posts: 7
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Mar 24, 2005
Posts: 186
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

Joined: Apr 01, 2007
Posts: 7
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

Joined: Dec 11, 2004
Posts: 13410

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finding mimetype using javascript