File Input accept every type of file, but not video and image - html

I want to implement an HTML5 file picker <input type="file" ...> which accepts all kinds of files.
But it should let the user select image/* and video/* files.
To be more precise: I'm trying to disallow images and videos. Anything else should be allowed.
The only way I currently see is to define a set of accepted file extensions (and/or mime types), but I don't think it's feasible to add a list of hundreds of mime types just to prevent that the user can select image and video files. So that wouldn't be a satisfying solution.
I'd prefer a JavaScript free solution, if possible.

Hope this Below link will help you in resolving your problem basically you need to add accept attribute to your input element and add the file types as the value which you want the user to upload.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file

Related

Limit the allowed file size for <input type=file> in pure HTML (no JS)

There are several topics* about setting up a max file size for a input type=file, but there is apparently no pure-standard HTML way to do so (nothing in the HTML spec).
So, is there any pure-HTML way to set a max file size on a input type="file"?
Is there a request for such feature, like a maxsize="..." attribute to let the browser filter out the files the user can selected, or do we have to stick to Javascript File API for years (or ever)?
*Topics:
JS File API: Limit the size of an file upload (html input) or How to check file input size with jQuery?
HTML never-saw-that-in-any-standard: HTML Upload MAX_FILE_SIZE does not appear to work
This is not part of the HTML spec, so there is no "pure HTML" way to do this.
I could only find this mention from 2013 with no reaction. Couldn't find any mentions in https://github.com/whatwg/html/issues
Sounds like it's something you could suggest, see FAQ - Is there a process for adding new features to a specification? for the recommended way to suggest new features to the working group. Please post the link to the discussion here if you do bring it up.
Though since there's a JS workaround, I guess this won't be high on anyone's to-do list.

My input file with accepts: accept="image/gif, image/jpg, image/jpeg, image/png", is allowing to select other extensions

I have a form to select images for a gallery, and I want to allow user to select only jpg,gif and png image formats.
And now, for testing, I change extension of a image to .bmp like "image1.bmp", and when I click in my input file to select a image, this "image1.bmp" is hidden, but If I select "show all files", this "image1.bmp" appears, and I can select this "image1.bmp" and send this image in my form. And Im inserting this image with this format on database.
This is normal using accept="image/gif, image/jpg, image/jpeg, image/png"??
Because what I wanted is to block all formats that are not gif,jpg or png.
I have this input file:
<input type="file" name="img[]" multiple="multiple" accept="image/gif, image/jpg, image/jpeg, image/png" />
This is common browser behavior. Browsers that support the accept attribute use it to create an initial file filter, but they do not prevent the user from changing or removing the filter and thereby selecting and submitting any file they like. The purpose of the attribute is to help users select files of appropriate types.
What browsers should do is less clear. HTML 4.01 says that the accept attribute “specifies a comma-separated list of content types that a server processing this form will handle correctly. User agents may use this information to filter out non-conforming files when prompting a user to select files to be sent to the server”. The reference to server-side processing may be misleading. The attribute affects client-side (browser) behavior only. The intent is to say that the attribute value should be written according to what file type(s) are expected from the user; it is more or less self-evident that the server-side form handler should be written so that it is capable of handling the specified type(s).
HTML5 LC is more verbose. It says that the attribute “may be specified to provide user agents with a hint of what file types will be accepted”. It then describes how it could be used by a browser to provide an adequate user interface. It also says: “User agents should prevent the user from selecting files that are not accepted by one (or more) of these tokens.” This might be sensible, but browsers do not actually do that. Even if they did, the attribute would not constitute a security measure of any kind (because a user could edit the form, or write a form of his own, or use a browser that ignores the accept attribute). Its purpose is to protect a normal user from making mistakes, like submitting a file of a type that will be rejected by the server-side handler.
(Browsers interpret the accept attribute value in rather simple way. They work on filename extensions, so if you name a GIF file, or a plain text file, or a binary program file so that its name ends with .png, they will treat it as a PNG image file, instead of inspecting the content of the file. The .bmp extension is problematic, since it commonly means Windows Bitmap, for which there is no registered MIME type; browsers may treat the nonstandard notation image/bmp as corresponding to .bmp.)
You cannot block sending of files. What you can do is to deal with files properly server-side, and there you should not of course rely on filename extensions but detect the types from the file content.
Clearly if you click "show all" can obviously see other files.
Your question is not quite hide or show, but filter at the time of upload, you have two solutions:
1) SERVER-SIDE:
With php (just an example) and regExp:
if (preg_match('#^image\/(png|)$#', $_FILES[$i]['img']['type']) === false) {
echo 'Invalid extension!';
} else {
//Save file
}
2) CLIENTE-SIDE:
With javascript use determinater lib:
https://github.com/rnicholus/determinater
Changing the extension does not change the mimetype of the file. Do the same test with an actual BMP file.
The accept attribute is not widely accepted. Chrome and IE10+ support it. If you're using anything else, like Firefox, Safari, or Opera, it won't work. You can read more here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input
This means that like Guilherme suggested, you'll need a client side or server side check. I'd suggest both. Client site will immediately tell an unsuspecting user, while the server side will help filter malicious users. However, beware, there is some debate on how MIME type detection isn't exactly reliable. You can Google around if you want to find out more about that.

How to upload images with same extension at one time HTML5

Using HTML5 upload multiple is it possible to have a wildcard upload. Means if user gave *.jpg in browse option is there any possibility to upload all the images that having the .jpg extension.
<input id="upload" type="file" multiple />
I have researched about it and couldn't found any thing.
No, the multiple attribute will let users select multiple files to upload to you, but they still need to select them themselves.
The downvoting might be because you're asking a question that has no reason for an answer!
Now look why?
<input id="upload" type="file" multiple />
This attribute here is meant to be selecting multiple files, doesn't include which type of files. So you can just filter out either images, or you can select audio, how? Here is code
<input id="upload" type="file" multiple accept="image/*" />
This will let the File Selector, only let the user select the files that are images.
Now to go further deep in the coding and selecting out images that have only the perfect type of format that you want. You'll require a server-side coding. Then at the serverside you can check for the fileExtension for each file selected, and save each file that has .jpg file extension and leave others without saving them.
However, not going in the bottom and just check each file in JS, you can use jQuery to check their fileExtension, split their names at '.' and check the last value that you get.
This way, you will get the required result. But multiple doesn't do that.

Setting default file types for the html input:file

Can I set default file types association for HTML file input? E.g. just .jpg,.bmp/.png with the HTML input control? When browse is clicked the dialog should show just show all those associated filetypes not allfiles that usually occurs.
The accept attribute of the HTML <form> element is meant for that, but this optional attribute is ignored by almost all webbrowsers. The answer is Flash or Java Applet. For both there exist 3rd party file upload plugins. E.g. Uploadify, SWFUpload and JumpLoader. Uploadify has my recommendation.
No, this is not possible.
One a user selects a file you can check the file type (extension) in javascript and decide on the basis of that whether to post the form or not.
However, since javascript can be subverted or turned off, you will have to check server side for the file types you allow, just to be certain.

controlling file types in HTML file input/upload dialog

Is there a way to specify the file types which can be accepted by HTML File Input/Upload dialog?
<input type="file" value="Upload XML file"/>
I wish to control that only certain file types are visible in the dialog based on their extensions.
Take a look at this answer -> File input 'accept' attribute - is it useful?
The accept attribute of input element was meant to provide mime-type filtering, however whilst the accept attribute exists within the RFC 1867, it would appear that the majority of browsers do not use this.
You will have to look for a JavaScript / Flash alternative, as suggested in the referenced post.