Farouk Alhassan
Farouk Alhassan

Reputation: 3808

java create a custom file extension that my application can recognize

I want to define a custom file format that my application can recognize. The file will be effectively an archive but I want the file extension to be different and also with a different mimetype. The content of the archive will be in a specific structure so I can easily find the entries.

Also, when a user is uploading, I want the browser to filter the directory and only show only files with this extension.

What is the best approach to address this problem.

Upvotes: 1

Views: 1773

Answers (2)

Andrzej Doyle
Andrzej Doyle

Reputation: 103827

It depends mainly on what you mean by "recognise".

For instance, in the general sense, you don't need to do much work at all. If you define something called *.qip which has the same general structure as a ZIP file, then all you have to do is have your application read that file as if it were a standard zip file. The file name (and to a lesser extent, MIME type) have no bearing at all on how your application handles the file - it will do so exactly as you've coded it to do so.

Now if you've coded your application to use a third-party library to do this file handling, you'd need to look at the library docs as this will vary as to how to achieve it (if it's supported at all).

As for the file upload form field, that's trickier. Theoretically you can specify a MIME type filter using the accept attribute, but that's ignored by I think just about every major browser out there. In light of that, I don't think there's anything robust that you can do client-side to check this - but hey, at least no-one else can do this either. And you are checking the validity of the files on the server-side, right? :-)

Upvotes: 1

aioobe
aioobe

Reputation: 421150

There is an "accept" attribute of the form-tag, that can specify mime-types of accepted files.

See thins w3schools article: http://www.w3schools.com/TAGS/att_form_accept.asp

Example:

<form action="form_action.asp" accept="image/gif, image/jpeg">
  First name: <input type="text" name="fname" /><br />
  Last name: <input type="text" name="lname" /><br />
  Your image: <input type="file" name="pic" id="pic" /><br />
  <input type="submit" value="Submit" />
</form>

(Does not seem to be supported by any major browsers though!)

Upvotes: 1

Related Questions