Searching the variable letters after the point to validate upload in Dropzone

3

I'm having a problem, I'm actually developing an image upload system and I'm using the dropzone , however I need to do something to separate the files that I have, in real I can only accept images of type jpeg, png, jpg. For this I'm getting the name of the file uploaded example: blablabla.jpeg I need to get everything after the point or need to return it to me, jpeg . So I make a case, and if that's not what I upload, I'll cancel it.

How to do this (get everything after. !ponto ) in javascript?

Or will the dropzone system really solve this?

    
asked by anonymous 13.02.2016 / 02:49

2 answers

3

If it is to get the extension of a file remember names may have a dot before the extension what your script will do ( link ) fails.

You can do it this way:

var v = "ARQUIVO.1.2.3.4.PNG";
var splt = v.split(".");

document.getElementById("nome").innerHTML = v;
document.getElementById("extensao").innerHTML = splt[splt.length - 1];
<div id="nome"></div>
<div id="extensao"></div>

If you want to remove the name extension you can use regex with .match :

var v = "ARQUIVO.1.2.3.4.PNG";
var splt = v.match(/(.*?)\.([a-zA-Z0-9]+)$/);

document.getElementById("nomecompleto").innerHTML = splt[0];
document.getElementById("nome").innerHTML         = splt[1];
document.getElementById("extensao").innerHTML     = splt[2];
<div id="nomecompleto"></div>
<div id="nome"></div>
<div id="extensao"></div>

Using the backend

But pay close attention, this type of checking is not secure, anyone can rename a file to an invalid extension. I do not know which language to use, but it's best to check mimetype, for example:

Using Dropzone.js

As you are using Dropzone.js create a javascript for this is totally unnecessary, the dropzone itself has the option of validation, see the documentation link

Use this to accept only .jpg, .jpeg and .png (I do not know if the check is safe or is done by mimetype, however it creates the server side check as above):

var myDropzone = new Dropzone("#meuid", {
    url: "URL",
    acceptedFiles: "image/jpeg,image/png"
});

Only JPG:

var myDropzone = new Dropzone("#meuid", {
    url: "URL",
    acceptedFiles: "image/jpeg"
});

Or so for all types of images:

var myDropzone = new Dropzone("#meuid", {
    url: "URL",
    acceptedFiles: "image/*"
});
    
13.02.2016 / 03:56
1

I found the solution, just do

var split = varivel.split(".");

then to appear everything I have after the dot I did:

split[1]

Below is an example of how to use it.

var v = "ARQUIVO.PNG";
var splt = v.split(".");

$(".anterioro").html(v);
$(".posterior").html(splt[1]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="anterioro"></div>
<div class="posterior"></div>
    
13.02.2016 / 03:03