aspose file tools*
The moose likes Servlets and the fly likes Restricting user to upload a CSV file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Restricting user to upload a CSV file" Watch "Restricting user to upload a CSV file" New topic
Author

Restricting user to upload a CSV file

Snehasish Jana
Greenhorn

Joined: Oct 06, 2011
Posts: 1

I want to restrict user only to upload CSV files in a perticular page.
I have tried with <input type="file" accept="text/csv" name="uploadCSV"> . But its not showing any filter. Is it possible to set a filter in HTML tag Or I need to write a javascript to validate that?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
JavaScript has no access to the file contents. And even if the "accept" worked, the user could upload a non-CSV file simply by giving it a .csv extension. No, you'll need to do the validation on the server.
Geeta Puttappanavar
Ranch Hand

Joined: Jan 18, 2012
Posts: 43
Hi Tim,

We too have the same the requirement.

Could you please tell me how to validate on the server using Spring?

Thanks,
Geeta
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Just to share my experience , I am using FilePicker.io in our application because, and its doing the job wonderfully. At some places I have to restrict users with file-extensions, and in some places of the application , users are restricted to a certain file size. And all the validation is done by filepickerio itself on the client side. Our main reason for using this was on the video upload module. We only accepted .mp4 and if user selected a large file (around 250mb with anyother extension) , then it was sheer stupidity to upload the 250mb file then perform validation of extension on the servlet side. FilePicker IO performs basic validation on the client side.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Geeta Puttappanavar
Ranch Hand

Joined: Jan 18, 2012
Posts: 43
Hi Asif,

Thank you so much for the reply.

Can you please provide me some example? And

I request you to suggest me the validation using Spring.
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

Hello Geeta,

In this case you can check for the file extension once the user uploads the file and validate the same using the Java File API

You can also add a "file type" to the dialog box that opens up when you click to upload a file, i.e. using <input type="file">, i hope you are using this.

You can use the accept keyword of the <input> tag of HTML

try this : <input type="file" accept=".csv"/>

But this will only add a specific file type to the dialog box and won't restrict the entry to a single type.

hope this helps.



http://plainoldjavaobject.blogspot.in
Geeta Puttappanavar
Ranch Hand

Joined: Jan 18, 2012
Posts: 43
Hi Lalit,

Thanks for the quick reply.

I tried this

<form:input path = "file" type = "file" accept=".csv"//>

But I am getting 500 exception

org.apache.jasper.JasperException: /pages/FileUploadForm.jsp(16,60) Unterminated <form:input tag...........................


Again I tried by using regular expression which I got it from some forum



public class FileUploadController {
private static Pattern fileExtnPtrn = Pattern.compile("([^\\s]+(\\.(?i)(|csv|))$)");

boolean isCSV = false;
if(match.matches()){
isCSV = true;
}
else {
result.rejectValue("file", "please enter Csv File", "please enter Csv File");
}
}

This is working fine ..
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

The exception is self explanatory. It says your tag is not closed. Now carefully read the end tag, any JSP tag starts with <> and ends with </>.


SCJP, SCWCD.
|Asking Good Questions|
Geeta Puttappanavar
Ranch Hand

Joined: Jan 18, 2012
Posts: 43
Hi Amit,

Thank you. Now its working
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

You are welcome.

Happy ranching .
 
wood burning stoves
 
subject: Restricting user to upload a CSV file