I have a Following JSP:
<form action="UploadServlet" method="post" enctype="multipart/form-data">
Select file to upload: <input type="file" name="file" id =upfile"size="50" /> <input type="button" value="Save" onclick="javascript:uploadPartnerDetails();" class="buttons">
</form>
And DWR Script which calls remoted Java Method:
function uploadPartnerDetails() {
SMUDWR.uploadPartnerDetails(function(data) {
dwr.util.setValue("UserTypeDiv", data, {
escapeHtml: false
});
});
}
The Remoted Method uploadPartnerDetails()is:
try {
WebContext wctx = WebContextFactory.get();
HttpServletRequest request = wctx.getHttpServletRequest();
// checks if the request actually contains upload file
if (!ServletFileUpload.isMultipartContent(request)) {
// if not, we stop here
}
I get This Error :
the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is text/plain
The Problem is the request above does not contain multipart/form-data.
How to I get that from this request?
In general DWR calls are AJAX calls and in AJAX we cannot upload files just like form data.
There are different ways to upload files using AJAX calls
Find different ways provided in answers to this question jQuery Ajax File Upload
Go through these and you will get better understanding of file handling with AJAX
Related
I have the following method in my controller
#RequestMapping(value = "processPurchase/{poid}", method = RequestMethod.DELETE)
public String processOrder(#PathVariable int poid) {
// do some processing
return acceptPurchaseForm;
}
My HTML
<form id="purchase-list-form" class="form-horizontal" action="/MyNewApp/processPurchase/" method="post">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="poid" value="">
With the above I still get the following error
WARN : org.springframework.web.servlet.PageNotFound - Request method 'DELETE' not supported
Any help appreciated.
First of all, I assume you have the HiddenHttpMethodFilter configured in your web.xml. It is required to convert your _method with value delete to the DELETE RequestMethod
Secondly, the poid is being passed in the body of the request but in your controller, you are expecting it to be passed in the URL itself. This might explain why Spring is unable to map the request.
EDIT 1:
To pass poid in URL, you will have to include in your form action when your HTML is generated. It depends on your view technology (I use Freemarker) but you would be required to do something like this:
<form action="/MyNewApp/processPurchase/${poid}" method="post">
Assuming that the poid is written to the model that is binded to your view.
I'm trying to do a file upload with JSP / JQuery but I have to make a few Ajax calls before submission.
However, after the Ajax calls are made and the form is submitted the form fields are all empty on the server side?
If I don't call e.preventDefault() then it all works but I need to make Ajax calls pre-submission!
Thanks
CLIENT SIDE:
<form method="post" action="accept.htm" enctype="multipart/form-data">
...
<input type="file" name="thefile" id="thefile"/>
<input type="submit" name="uploadfile" id="uploadfile" value="Upload File"/>
...
</form>
$("form").on("submit", function(e)
{
e.preventDefault(); // stop the form being submitted for now
// make a few ajax calls
...
// submit the form in ajax success callback
$("form").unbind("submit");
$("form").submit();
}
SERVER SIDE:
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
ServletFileUpload servletFileUpload = new ServletFileUpload();
servletFileUpload.setFileSizeMax(Long.valueOf(maxUploadFileSize));
servletFileUpload.setProgressListener(new UploadProgressListener());
FileItemIterator fileItemIterator = servletFileUpload.getItemIterator(request);
if(isMultipart)
{
while(fileItemIterator.hasNext()) <--- empty?
{
...
}
}
try this
$("form").on("submit", function(e)
{
// make a few ajax calls
...
// finally submit
$("form").unbind("submit");
$("form").submit();
e.preventDefault(); // stop the form being submitted for now
}
e.preventDefault(); send return false may issue with ajax
Turns out I had a JQuery statement in one of the Ajax calls that disabled the file input field. When this was removed the file became available again on the server side. So disabling the file input field causes it to become unavailable for some reason.
I am trying to find a way to invoke a piece of java code within the JSP using HTML form
<form method="get" action="invokeMe()">
<input type="submit" value="click to submit" />
</form>
<%
private void invokeMe(){
out.println("He invoked me. I am happy!");
}
%>
the above code is within the JSP. I want this run the scriptlet upon submit
I know the code looks very bad, but I just want to grasp the concept... and how to go about it.
thanks
You can use Ajax to submit form to servlet and evaluate java code, but stay on the same window.
<form method="get" action="invokeMe()" id="submit">
<input type="submit" value="click to submit" />
</form>
<script>
$(document).ready(function() {
$("#submit").submit(function(event) {
$.ajax({
type : "POST",
url : "your servlet here(for example: DeleteUser)",
data : "id=" + id,
success : function() {
alert("message");
}
});
$('#submit').submit(); // if you want to submit form
});
});
</script>
Sorry,not possible.
Jsp lies on server side and html plays on client side unless without making a request you cannot do this :)
you cannot write a java method in scriptlet. Because at compilation time code in scriptlet becomes part of service method. Hence method within a method is wrong.
How ever you can write java methods within init tag and can call from scriptlet like below code.
<form method="get" action="">
<input type="submit" value="click to submit" />
</form>
<%
invokeMe();
%>
<%!
private void invokeMe(){
out.println("He invoked me. I am happy!");
}
%>
Not possible.
When the form is submitted, it sends a request to the server. You have 2 options:
Have the server perform the desired action when the it receives the request sent by the form
or
Use Javascript to perform the desired action on the client:
<form name="frm1" action="submit" onsubmit="invokeMe()"
...
</form>
<script>
function invokeMe()
{
alert("He invoked me. I am happy!")
}
</script>
You can't do this since JSP rendering happens on server-side and client would never receive the Java code (ie. the invokeMe() function) in the returned HTML. It wouldn't know what to do with Java code at runtime, anyway!
What's more, <form> tag doesn't invoke functions, it sends an HTTP form to the URL specified in action attribute.
for me it seems impossible but expecting clarification on it. i am sending a request as follow :
<form action="/name" method="get">
<input type="text" />
<input type="submit" />
</form>
Now action class at server side manipulates & send the response to client, can i handle this response by ajax somehow ??
Yes, but you have to submit it via ajax (XmlHttpRequest) in order to be able to get the response that way.
Using jQuery makes this simple:
$.post("/name", {param:param}, function(data) {
});
In that example you should pass manually each form field as param. In case of bigger forms this is not that good. So you can use serialize():
$.post($("#yourForm").attr("action"),
$("#yourForm").serialize(),
responseHandlerFunction);
I'm trying to implement a workaround to the problem of using AJAX with multi-part form data. This page looks like a good idea. Bascially, instead of using AJAX you redirect the output of the form submission to an iframe. However, it isn't quite working for me. Here's my client-side HTML code:
<form id="submitDocumentForm" target='upload_target' name="submitDocumentForm" enctype="multipart/form-data" action="MyServlet" method="POST">
<input id="inputFile" type="file" name="inputFile"/>
<input type="submit" value="Import Document" />
<iframe id="upload_target" name="upload_target" src="" style="width:100;height:100;border:0px solid #fff;"></iframe>
</form>
The server side processing seems to work fine, but the client side response (IE7) is to pop up a File Download "Do you want to save this file, or find a program..." dialog box. Here's the relevant server side Java code:
//handling of the file and exception handling omitted
JSONObject responseData = new JSONObject();
responseData.put("messages", "Upload complete");
PrintWriter pw = response.getWriter();
response.setHeader(RESP_HEADER_CACHE_CONTROL, RESP_HEADER_VAL_NO_CACHE);
response.setContentType("application/json");
pw.println(responseData.toString());
pw.close();
Any suggestions as to how I might fix this? Many thanks in advance.
Set the response's Content-Type to text/plain to force the browser to treat it as a document instead of a file.