Upload specific files only - html

I would like to limit the user to upload only specific files.I know it can be done through php script which the form action value is leading to but that's not what I'm looking for.
When the user is choosing a file after using that line:
<input type="file" ... />
He got an option to select all files
I want to change it only to a specific extensions.

You could use the accept attribute. For example if you wanted to limit to all image files:
<input type="file" accept="image/*" />
You could also specify a list of possible types:
<input type="file" accept="image/*,video/*" />
Needless to say that legacy browsers that do not support this new attribute will behave exactly as shown in your screenshot and completely ignore this attribute and there's nothing you could do about it.
In all cases you should provide a server side validation of the actual file type being uploaded and never rely on the client to ensure this.

Related

Filetype for .log file when using HTML5 element input type="file"

I was playing around with using the <input type="file" /> element and I have it accept a variety of different file types I wish to support.
<input type="file" name="file" accept="image/png, image/jpeg, image/jpg, application/pdf, application/txt, text/plain, application/pdf, text/x-log, application/enex+xml, text/*, .log" />
Depending on the file that is served, I can see what file type is passed by looking at the FileList which looks like this.
Everything works fine but for .log, as you can see above, doesn't have a type... I was wondering if there was a way to discern .log files? I could look at the name passed and parse the file extension, but was wondering if there was a better way? I'm also uncertain of what other file types do not have a registered type and if there is documentation on that?
Unfortunately you can't rely on the type returned. The available types will change depending on the browser and operating system. Generally it returns values registered with the os.
You can use the file name but that's not 100% reliable either. There's nothing preventing a user from renaming a zip file to .log. In most cases that will change the mime as well. The only way to be 100% sure is to read the file and see what it contains.

html5 pattern validation for file not working

I searched in Google there has no option for validate file by using URL pattern.
I also have tried in my site. But pattern for file not working. Check my code -
<input type="file" class="form-control" id="mp3file_name" name="mp3file_name" pattern="([^\s]+(\.(?i)(mp3))$)" accept="audio/mpeg, audio/mp3" required="required" />
Attribute accept working fine but I need to validate If I will uploading other files except mp3
Remove the pattern and just use:
<input type="file" class="form-control" id="mp3file_name" name="mp3file_name" accept="audio/mpeg, audio/mp3" required="required" />
Also as said by Mave:
The accept attribute is incredibly useful. It is a hint to browsers to only show files that are allowed for the current input. While it can typically be overridden by users, it helps narrow down the results for users by default, so they can get exactly what they're looking for without having to sift through a hundred different file types.
Please see File input 'accept' attribute - is it useful?:
The accept attribute is incredibly useful. It is a hint to browsers to only show files that are allowed for the current input. While it can typically be overridden by users, it helps narrow down the results for users by default, so they can get exactly what they're looking for without having to sift through a hundred different file types.
You can make it easier for users to browse through their files, but ultimately you have no control over it, as it is user input. Make sure you check the file in whatever language you receive the form in.

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.

How to restrict <input type="file"> so that it can only select .pdf files?

By default it can select all type of files,how to restrict it so that it can only select .pdf files?
more explicitly...
<input type="file" accept="application/pdf" />
You can use the accept attribute on your form to suggest to the browser to restrict certain types. However, you'll want to re-validate in your server-side code to make sure. Never trust what the client sends you.
Simply put: you can't using the plain html and javascript. The closest you can get is to test the file extension using javascript before submitting the form and show some error message to the user if it is other than .pdf. You might need to use some client side solution such as Flash upload controls if you want to achieve this.

attaching a file path in a html form

<form>
<label for="attachment">Attachment:</label>
<input type="file" name="attachment" id="attachment">
<input type="submit">
</form>
I want to attach the file path to a form. I am doing it using the following code, but I want that the pop up window should open to a specified path, say D:\newfolder, so the user doesn't need to go to D: and then newfolder to attach the file.
Is there any way I can set this predefined path?
This can't be done in any browser for security reasons.
As far as I know, It's also not possible using a Flash-based uploader like SWFUpload.
You may be able to pre-set the path using an alternative Java-based uploader that has more liberal access to the client's computer, but this feature is hardly worth making the switch to that technology and the additional requirements and hassles it brings along.