0

I'm working with an upload script,these lines of codes are not working.Here 'else if' condition doesn't works.It does not showing txt_file.png as a preview of file if a text file uploaded.

if (!file.type.match(/ image.*/)) {
    if (file.type.match(/ *.avi/)){
        myDropzone.emit("thumbnail", file, "images1/video_icon.png");
    } else if  (file.type.match(/ *.txt/)){
        myDropzone.emit("thumbnail", file, "images1/text_icon.png");
    }
    ....
}

Am i doing anything wrong ?Pls help me.Thank you..

6
  • 1
    what is not working exactly? Commented Mar 8, 2014 at 15:58
  • 1
    Define "not working". Commented Mar 8, 2014 at 15:59
  • 1
    do you have any error in the chrome's console? Commented Mar 8, 2014 at 15:59
  • @sabotero I'm wondering why exactly chrome lol Commented Mar 8, 2014 at 16:01
  • else if condition doesn't works.It does not showing txt_file.png as preview of file if a text file uploaded. Commented Mar 8, 2014 at 16:02

2 Answers 2

4

Regular expressions are not correct. Change

/ *.txt/

to

/.*\.txt/

or event better /.*\.txt$/.

Character * has a special meaning in regular expressions. So it has to be escaped if you use it like you did (however it would not work as expected anyway, because it would not match file extensions).

The same with *.avi. Also you would probably want to use test for this task.

More information here: Regular Expressions.

Sign up to request clarification or add additional context in comments.

2 Comments

Oopz this is also not working.. :( ..In my previous code it shows video_icon if a .avi file uploaded but not showing text file icon if a text file uploaded..
Hm, so the problem somewhere else. The code should work. jsfiddle.net/VP77t Make sure file.type is accurate.
0

I would prefer to use the .search() function on a string and not the .match() because you don't care about a matching result you only need a boolean condition.

if (file.type.search(/\.avi/) !== -1){
    myDropzone.emit("thumbnail", file, "images1/video_icon.png");
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.