Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!

kalai rathi

Greenhorn
+ Follow
since Aug 16, 2011
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by kalai rathi

Hello,

I am using Spring MVC. i have some form elements and image upload. before submit button i have to validate the image
(type, Height & width and size).
only accept JPG,JPEG,PNG,GIF file formats
400*400 ---height * width of the image
10000---size of the file.

Using jquery& ajax how to validate the image file?.Please help me. very Urgent Please

the project Details are as follows,

I have a form with some input elements and image upload button. using Spring MVC i validated the form elements and inserted into sql Database. successfully i completed. The modified requirement is before click a submit button i have to validate the image


instead of spring Validator i want to use Jquery -Ajax.

But here i attach sample program which i was tried to call spring mvc. i couldn't get the result. please any one try to help me.


here is my Controller

package test.mydir;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class TestBeanController extends SimpleFormController {

@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
TestBean testBean=(TestBean)command;
String name=testBean.getName();

System.out.print(name);
return new ModelAndView("success");


}

Here is My Service Class

package test.mydir;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class TestBeanService extends SimpleFormController {
private static final Logger log = Logger.getLogger(TestBeanService.class);

protected boolean isFormChangeRequest(HttpServletRequest request)
{
log.info("isfileupload method");
return isFileUploadRequest(request);

}


private boolean isFileUploadRequest(HttpServletRequest request) {

log.info("isfileupload method body");
return (!request.getParameter("name").equals(null));

}

protected void onFormChange(HttpServletRequest request,HttpServletResponse responses,
Object command,BindException bind){

TestBean testBean=(TestBean)command;
log.info("inside formchange");
if(isFileUploadRequest(request))
{
PrintWriter out=new PrintWriter(System.out);
out.print(testBean.getName());
System.out.print("testBean.getName");

}


}


}

Here is my Bean

package test.mydir;

public class TestBean {
private String name;
private String name1;

public String getName1() {
return name1;
}

public void setName1(String name1) {
this.name1 = name1;
}

public TestBean() {

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


}


here is my Jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"; %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">;
<html>
<head>
<script type="text/javascript" src="<%=request.getContextPath()%>jquery-1.6.2.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.form.js"></script>
<script>
function call(){

var queryString = $('#myForm').formSerialize();

$.post('testBeanService.html', queryString,
function(data){
alert(data);
});
}
</script>

</head>
<body>
<form:form id="myForm" method="post" commandName="event">
Name: <form:input path="name" id="name" onchange="call();" />
<form:input path="name1" id="name1" />
<input type="submit" value="Submit Comment" />
</form:form>
</body>
</html>

Here is my DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<beans>


<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"></property>
</bean>

<bean name="/testBeanService.html" class="test.mydir.TestBeanService"/>

<bean name="/eventCreation.html" class="test.mydir.TestBeanController">
<property name="commandName" value="event"/>
<property name="commandClass" value="test.mydir.TestBean"/>
<property name="formView" value="test"/>
<property name="successView" value="success"/>
</bean>

</beans>





Thanks

9 years ago
Hello,
I am new to Spring MVC

I am uploading image into DataBase. Before uploading the image,validate the image file format .
The file format should be (*.png,*.jpeg,*.jpg,*.gif) ,for other types must show error. this is the logic.

this is my jsp
<form:form commandName="event" id="eventForm" name="eventForm" method="post" enctype="multipart/form-data">

<label>Event Photo</label>
<input type="file" name="eventPhoto"/>
<form:errors path="eventPhoto" cssClass="error" />
</form>

My validation here.

if((!eventForm.getEventPhoto().getContentType().equals("image/png"))&&
(!eventForm.getEventPhoto().getContentType().equals("image/x-png"))&&
(!eventForm.getEventPhoto().getContentType().equals("image/jpeg"))&&
(!eventForm.getEventPhoto().getContentType().equals("image/pjpeg"))&&
(!eventForm.getEventPhoto().getContentType().equals("image/jpg"))&&
(!eventForm.getEventPhoto().getContentType().equals("image/gif")))
{
errors.rejectValue("eventPhoto", "filetype.required");
}

the above code has worked in Safari, Firefox, Chrome. Except IE


when i upload *.bmp image in IE it accepts as --- image/pjpeg
.ico--------image/x-icon

it doesn't show error. IE accepts all image formats. This is the problem.

problem 2)

validate the image dimension ( height & weight) like 400*400

Thanks
9 years ago