Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
Nikkelmann  
#1 Posted : Wednesday, January 11, 2012 1:31:31 AM(UTC)
Nikkelmann

Rank: Member

Groups: Member
Joined: 11/29/2011(UTC)
Posts: 10

Thanks: 2 times
Hi!

I need to validate the file name of each file before it is added to the upload queue, however I can't find an event for this, so I have tried with the uploadFileCountChange event, but this event happens AFTER the files have been added to the queue.

I have tried to remove the event and add it again after I have looped through all the files, but this does not work; the event gets called for each file I remove.

Is there any way to apply regex validation to file names on the client side?

(I have validation on the server side atm but there is no need for the client to upload files that are not valid).

Code:

var CsvRegex = /\d+-\d+(?:\s+.*)?\.csv$/i;

function uploadFileCountChangeHandler() {
	// Validate the file before uploading it.
	// If the file is rejected, add it to the post as a string.
	var iu = $au.uploader('auIU');
	
	// Remove the event for the moment
	iu.events().uploadFileCountChange().remove(uploadFileCountChangeHandler);

	for (var i = 0, count = iu.files().count(); i < count; i++) {
		var file = iu.files().get(i);
		if (typeof file != 'undefined' && file != null) {
			var filePath = file.name(),
				fileExtension = filePath.substring(filePath.lastIndexOf(".")).toLowerCase(),
				fileValid = false;

			if (fileExtension == ".csv") {
				fileValid = CsvRegex.test(filePath);
			}
			
			if (!fileValid) {
				// Run some other code to add the file to the post, then remove the file from the upload queue.
				file.remove();
				i--;
			}
		}
	}

	// Add the event again
	iu.events().uploadFileCountChange().add(uploadFileCountChangeHandler);
}

The image uploader has these settings (partial)

Code:

...
restrictions: {
	fileMask: '*.jpg;*.jpeg;*.png;*.pdf;*.csv'
},
converters: [
	{ mode: '*.*=SourceFile' }
],
events: {
	uploadFileCountChange: uploadFileCountChangeHandler
},
...

Edited by user Monday, January 16, 2012 1:08:35 AM(UTC)  | Reason: Changed [code] to [code=js]

Dmitry.Obukhov  
#2 Posted : Wednesday, January 11, 2012 2:11:01 AM(UTC)
Dmitry.Obukhov

Rank: Advanced Member

Groups: Administration
Joined: 5/29/2010(UTC)
Posts: 1,310

Thanks: 8 times
Was thanked: 111 time(s) in 111 post(s)
Hello,

Unfortunately, Image Uploader does not have event which will fire before adding files to upload pane. Could you please describe your needs in details? Why do you need to validate files before adding?

Best regards,

Dmitry Obukhov

Technical Support. Aurigma, Inc.

Nikkelmann  
#3 Posted : Wednesday, January 11, 2012 2:45:53 AM(UTC)
Nikkelmann

Rank: Member

Groups: Member
Joined: 11/29/2011(UTC)
Posts: 10

Thanks: 2 times
I use the Image Uploader to upload folders that have a specific structure.

The files have to conform to this specific structure and all the file names have a specific expression, fx: csv files are like this 123-456.csv and 35-326 extra.csv and so on.

Edited by user Wednesday, January 11, 2012 2:46:55 AM(UTC)  | Reason: Not specified

Dmitry.Obukhov  
#4 Posted : Wednesday, January 11, 2012 7:17:43 PM(UTC)
Dmitry.Obukhov

Rank: Advanced Member

Groups: Administration
Joined: 5/29/2010(UTC)
Posts: 1,310

Thanks: 8 times
Was thanked: 111 time(s) in 111 post(s)
Quote:
I use the Image Uploader to upload folders that have a specific structure.

The files have to conform to this specific structure and all the file names have a specific expression, fx: csv files are like this 123-456.csv and 35-326 extra.csv and so on.

Thank for this explanation. Could you please explain also why you need to check and validate these files before adding them to upload pane?

Best regards,

Dmitry Obukhov

Technical Support. Aurigma, Inc.

Nikkelmann  
#5 Posted : Thursday, January 12, 2012 12:37:18 AM(UTC)
Nikkelmann

Rank: Member

Groups: Member
Joined: 11/29/2011(UTC)
Posts: 10

Thanks: 2 times
Thank you for taking your time to read what I write.

Because the files are rather large and there are many, there is no reason to upload files that are not needed.

The perfect thing for this would be an BeforeValidation event where I could validate the files using javascript.

This is an example of the workflow:

- The user opens the website and navigates to the Image Uploader.

- The user selects an folder which has all the files that has been exported by a 3rd party software program to a folder with a specific structure.

- The Image Uploader validates all the files with regex to ensure no unneeded files are uploaded.

- The user clicks upload and all the files that passed the validation are uploaded.

- The webserver processes the files and stores the result in the database and on the fileserver.

- Ther user closes the website.

Edited by user Thursday, January 12, 2012 4:01:04 AM(UTC)  | Reason: Spelling correction

Dmitry.Obukhov  
#6 Posted : Thursday, January 12, 2012 6:27:05 PM(UTC)
Dmitry.Obukhov

Rank: Advanced Member

Groups: Administration
Joined: 5/29/2010(UTC)
Posts: 1,310

Thanks: 8 times
Was thanked: 111 time(s) in 111 post(s)
Hmm, probably does it make sense to set file mask and do not upload *.csv files at all if you are saying that they are not needed to be uploaded?
Best regards,

Dmitry Obukhov

Technical Support. Aurigma, Inc.

Nikkelmann  
#7 Posted : Friday, January 13, 2012 3:06:12 AM(UTC)
Nikkelmann

Rank: Member

Groups: Member
Joined: 11/29/2011(UTC)
Posts: 10

Thanks: 2 times
I already use the file mask and I need the .csv files, just not all of them.

Example:

123-234.csv - Valid. (Upload)

231-213 names.csv - Valid. (Upload)

files.csv - Not valid! (This file should not be uploaded)

(I also validate the jpg, png and pdf files with each their regex, so this is not just for the csv files, they are just an example)

Edited by user Friday, January 13, 2012 3:43:50 AM(UTC)  | Reason: Spelling correction and added explanation.

Dmitry.Obukhov  
#8 Posted : Sunday, January 15, 2012 8:20:55 PM(UTC)
Dmitry.Obukhov

Rank: Advanced Member

Groups: Administration
Joined: 5/29/2010(UTC)
Posts: 1,310

Thanks: 8 times
Was thanked: 111 time(s) in 111 post(s)
Hello,

Thank you for your details. I implemented simple code snippet based on UploadFileCountChange event. It checks file name (validating), and removes invalid file trough remove() method:

Code:

function UploadFileCountChange(){
  var invalid = "123.csv";
  count = $au.uploader('Uploader1').files().count();
  for (i = 0; i < count; i++){	
    name = $au.uploader('Uploader1').files().get(i).name(); 					
    if (name == valid){
      //alert (name); 
      $au.uploader('Uploader1').files().get(i).remove();
    }
  }
}

I tested it locally, and it worked fine. Files not validated were removed from upload pane. You can check also files' sizes to validate them in the same way.

Edited by user Sunday, January 15, 2012 8:21:54 PM(UTC)  | Reason: Not specified

Best regards,

Dmitry Obukhov

Technical Support. Aurigma, Inc.

Nikkelmann  
#9 Posted : Monday, January 16, 2012 1:04:34 AM(UTC)
Nikkelmann

Rank: Member

Groups: Member
Joined: 11/29/2011(UTC)
Posts: 10

Thanks: 2 times
Dmitry.Obukhov wrote:
Hello,

Thank you for your details. I implemented simple code snippet based on UploadFileCountChange event. It checks file name (validating), and removes invalid file trough remove() method:

Code:

function UploadFileCountChange(){
  var invalid = "123.csv";
  count = $au.uploader('Uploader1').files().count();
  for (i = 0; i < count; i++){	
    name = $au.uploader('Uploader1').files().get(i).name(); 					
    if (name == valid){
      //alert (name); 
      $au.uploader('Uploader1').files().get(i).remove();
    }
  }
}

I tested it locally, and it worked fine. Files not validated were removed from upload pane. You can check also files' sizes to validate them in the same way.

That is the same as the code I posted in the first post.

This works, yes, but as I said before, this code causes the event "uploadFileCountChange" to get called EACH time a file is removed.

So if the user adds 100 files, where 10 are non-valid then the event is called 10 times or more.

In my bulk upload I have 2000 files where an estimated 100 files are non-valid.

That's why I tried to remove and add the event again like so:

Code:

// Remove the event for the moment
iu.events().uploadFileCountChange().remove(uploadFileCountChangeHandler);
.
.
// Remove the non-valid files here
.
.
// Add the event again
iu.events().uploadFileCountChange().add(uploadFileCountChangeHandler);

But the event still gets fired with each file removed.

Dmitry.Obukhov  
#10 Posted : Monday, January 16, 2012 1:50:56 AM(UTC)
Dmitry.Obukhov

Rank: Advanced Member

Groups: Administration
Joined: 5/29/2010(UTC)
Posts: 1,310

Thanks: 8 times
Was thanked: 111 time(s) in 111 post(s)
Actually, this event fires each time when a file is added to /or removed from upload pane. When files are valid, they are not removed, and you do not see any effect, but UploadFileCountChange event fires. Unfortunately, there is no way to handle your situation.
Best regards,

Dmitry Obukhov

Technical Support. Aurigma, Inc.

Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.