Handler execution resulted in exception: Required MultipartFile parameter 'file' is not present - javascript

My Controller code :
#RequestMapping(value = "/rest/auth/admin/test/responseTest", method = RequestMethod.POST)
#ResponseBody
public ResponseEntity<ResponseVO> responseTest(#RequestParam("file") MultipartFile file,
#RequestParam(value = "testId", required = true) long testId) {
I have already added the multipartResolver bean to my application-context.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
My javascript code :
var fd = new FormData();
fd.append('file', $('input[type=file]')[0].files[0]);
fd.append("label", "WEBUPLOAD");
var headers = {};
headers[Constants.XCSRFTOKENCookieName] = util.getCookie(Constants.XCSRFTOKENCookieName);
var url = "rest/auth/admin/test/responseTest?responseTest=123";
var dataType = "json";
var contentType = "application/json";
$.ajax({
url: url,
type: "POST",
data: fd,
dataType: dataType,
async: isAsync,
headers: headers,
enctype: contentType,
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function(result, status, jqXHR) {
resultObj = result;
if (successCallBack != null) {
successCallBack(resultObj);
}
//util.hideLoader();
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error : " + errorThrown);
resultObj = jqXHR.responseJSON;
if (errorCallBack != null) {
errorCallBack(resultObj);
}
//util.hideLoader();
}
});
When I am calling the above Ajax I am getting the following error at server side.
[org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] > logException() : 186 - Handler execution resulted in exception: Required MultipartFile parameter 'file' is not present
Please help me to resolve this issue.

I would have thought the contentType header for this should be multipart/form-data instead of application/json?

Related

Why does web service not return string properly?

I have ajax function that send some string to webservice.
Here is ajax:
var data = "wkt=" + wkt;
$.ajax({
url: "....some path",
type: "POST",
data: data,
crossDomain: true,
dataType: "text",
success: function (response) {
alert(response);
},
error: function () {
console.log('Request Failed.');
}
});
And here is web service:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class ValveService : System.Web.Services.WebService
{
[WebMethod]
public string ExecuteQuery(string wkt)
{
return "dummy!";
}
}
As response I get this string:
"<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">dummy!</string>"
While I expect to get as response "dummy!".
Any idea why I get this strange responce and how to get only string that was sent from service(in my case "dummy!") ?
I'm pretty sure web services only return xml or json. There might be a way around it, setting a response type in the service but I'm not sure. [Edit: I see Nerdi.org already hinted at this.]
When dataType: 'text', the response header is not just text, but Content-Type: text/xml; charset=utf-8, and you get xml.
Go for json (which is a string) and work with that.
//var data = "wkt=" + wkt;
$.ajax({
url: "/path to/ExecuteQuery",
type: "POST",
data: JSON.stringify({ wkt: wkt }),
contentType: "application/json; charset=utf-8", // this will be the response header.
crossDomain: true,
dataType: "json",
success: function(response) {
// response is a wrapper. your data/string will be a value of 'd'.
alert(response.d);
},
error: function() {
console.log('Request Failed.');
}
});
An alternative:
[WebMethod]
public void ExecuteQuery(string wkt)
{
Context.Response.Output.Write("dummy " + wkt);
Context.Response.End();
}

Return JSON instead of XML from web service using Ajax while 'contentType' is 'false'

I made an AJAX call to send an image file to one of my web service (.asmx) methods. Everything's okay, but the problem is that the web service returns XML instead of JSON because I HAVE TO set 'contentType' to 'false', otherwise file can't be sent. (If I set contentType to application/json; charset=utf-8, it returns JSON but I can't do that because I'm sending a file.)
This is my JavaScript:
function setAvatar(imageFile, successCallback) {
var formData = new FormData();
formData.append("UploadedAvatar", imageFile);
$.ajax({
type: "POST",
url: "/Services/UserService.asmx/SetAvatar",
contentType: false,
processData: false,
dataType: 'json',
data: formData,
success: function (result) {
alert(result.d);
alert(result.d.IsSuccessful);
if (typeof successCallback === 'function')
successCallback(result);
}
});
And the web service method:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Result SetAvatar()
{
HttpPostedFile postedFile = HttpContext.Current.Request.Files["UploadedAvatar"];
Image avatar = Image.FromStream(postedFile.InputStream, true, true);
avatar = new Bitmap(avatar, new Size(150, 150));
avatar.Save(Path.Combine(path, $"Avatar-Small.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
return new Result(true, Messages.AvatarSavedSuccessfully);
}
Set the Accept header when making the request to expect JSON
$.ajax({
type: "POST",
url: "/Services/UserService.asmx/SetAvatar",
headers: { //SET ACCEPT HEADER
Accept : "application/json; charset=utf-8",
},
contentType: false,
processData: false,
dataType: 'json',
data: formData,
success: function (result) {
alert(result.d);
alert(result.d.IsSuccessful);
if (typeof successCallback === 'function')
successCallback(result);
}
});
On the server side, using Json.Net you can serialize the result
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string SetAvatar() {
HttpPostedFile postedFile = HttpContext.Current.Request.Files["UploadedAvatar"];
Image avatar = Image.FromStream(postedFile.InputStream, true, true);
avatar = new Bitmap(avatar, new Size(150, 150));
avatar.Save(Path.Combine(path, $"Avatar-Small.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
var result = new Result(true, Messages.AvatarSavedSuccessfully);
return JsonConvert.SerializeObject(result);
}
This should allow the response to be in the desired type
You need to update your .NET code and add options.RespectBrowserAcceptHeader = true
services
.AddMvc(options => {
options.RespectBrowserAcceptHeader = true;
})
//support application/xml
.AddXmlDataContractSerializerFormatters()
//support application/json
.AddJsonOptions(options => {
// Force Camel Case to JSON
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
The dataType: 'json' will automatically add the Accept header as below
"accept":"application/json, text/javascript, */*; q=0.01"
If you specifically want just application/json, then you need to add below to your ajax options
headers: {
Accept : "application/json; charset=utf-8"
}
After beating my head against the wall for many days, I finally figured out the solution which belongs to Ajay, here.
While nothing else has helped me, I used this and it worked perfectly:
Change the return type of your method to void.
And then instead of writing the return statement in your method, you need to do this to return a value:
Context.Response.ContentType = "application/json; charset=utf-8";
Context.Response.Write(new JavaScriptSerializer().Serialize(new YourData()));
After that you can easily get the result using the success event of the Ajax call:
success: function (result) {
alert(result.Property); // Note: Don't use result.d here
}

Can't take file from server

I use Asp.Net WebApi and jQuery.
From page to WebApi i send 'POST' HTTP-Request with object.
$.ajax({
type: "POST",
url: "api/Result",
data: JSON.stringify(makeResultInfo()),//makeResultInfo - function that returns object
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
success: function (data) {
window.location = data;
alert("success");
},
error: function (xhr) {
alert('error');
}
});
WebApi take object correctly and return Excel-file.
[HttpPost]
public HttpResponseMessage Post([FromBody]ResultInfo value)
{
ExcelClass ec = new ExcelClass();
var stream = ec.GetStream(ec.mini_wb);//после теста поправить
// processing the stream.
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(stream.ToArray())
};
result.Content.Headers.ContentDisposition =
new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "test.xlsx"
};
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
return result;
}
When i make Request to 'api/Result' without parameters - file downloading correctly. But when i send object, response incorrect always and alert always write 'error'.
How i can send object and take file in browser?

not able to get JSON string in servlet

I am trying to pass some variables through JSON from JSP to servlet through ajax call. But i am getting null value at servlet side. Please some one help me on to find out where i am making mistake/ what i missed
//JSON
var masterdata = new Object();
masterdata.grn = $('#grn').val();
masterdata.pono = $('#pono').val();
masterdata.podt = $('#podt').val();
//call the servlet to insert the data only when error = 0
if (error != 1){
$.ajax({
url : 'insertserv',
type: 'POST',
dataType: 'json',
data: {test : JSON.stringify(masterdata)},
contentType: 'application/json',
mimeType: 'application/json',
success : function(data) {
alert('Hi');
}
});
}
else{
alert("Save cannot be performed. Please check the entered data!");
}
});
public class insertserv extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("I am inside insert");
String masterdata = request.getParameter("test");
System.out.println("masterdata : "+masterdata);
response.setContentType("text/plain");
}
}
Replace your ajax code with my code...
//JSON
var masterdata = new Object();
masterdata.grn = $('#grn').val();
masterdata.pono = $('#pono').val();
masterdata.podt = $('#podt').val();
//call the servlet to insert the data only when error = 0
if (error != 1){
$.ajax({
url : 'insertserv',
type: 'POST',
dataType: 'json',
data: JSON.stringify({"test" :masterdata}),
contentType: 'application/json',
mimeType: 'application/json',
success : function(data) {
alert('Hi');
}
});
}
else{
alert("Save cannot be performed. Please check the entered data!");
}
});
To get data in servlet
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = "";
if (br != null) {
json = br.readLine();
}
JSONObject wholedata= new JSONObject(json);
now the object wholedata has a your json..
if you are using JSON.stringify() then you have to use
BufferedReader in servlet ,
You can use request.getparameter in servlet when you are passing data in URL of servlet.
If your backend is responding with json content then only dataType:"json" works. Try change the response type:
response.setContentType("application/json");
Because your ajax is expecting json from the backend with dataType:"json",.
or vice-versa.
change the dataType to text: dataType:"text", as the response header says response.setContentType("text/plain");. But in this case you have to use JSON.parse() to parse the json string.
//JSON
var masterdata = new Object();
masterdata.grn = $('#grn').val();
masterdata.pono = $('#pono').val();
masterdata.podt = $('#podt').val();
//call the servlet to insert the data only when error = 0
if (error != 1) {
$.ajax({
url: 'insertserv',
type: 'POST',
dataType: 'text', //<------change this
data: {
test: JSON.stringify(masterdata)
},
contentType: 'application/json',
mimeType: 'application/json',
success: function(data) {
alert('Hi');
}
});
} else {
alert("Save cannot be performed. Please check the entered data!");
}
});

AJAX post JSON data arrives empty

This is my AJAX request
data = JSON.stringify(data);
url = base_url + "index.php/home/make_order";
//alert(url);
var request = $.ajax({
url: url,
type: 'POST',
contentType: 'application/json',
data: data
});
request.done(function(response){
alert('success');
});
request.fail(function(jqXHR, textStatus, errorThrown){
alert('FAILED! ERROR: ' + errorThrown);
});
My problem is that when it arrives to the PHP CI-controller $this->input->post('data'), it is empty.
This is my data: as shown before the AJAX request:
data = {"sum":"2.250","info":[{"id":"6","name":"bla","price":"1.000"}]}
First I'd like to thank all responses.
Actually it was a couple of mistakes,
First: as #bipen said, data must be sent as an object rather than a string. and when I tried it, it didn't work because I didn't put the single-quote around data
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json',
data: {'data': data}
});
Second: as #foxmulder said, contentType was misspelled, and should be ContentType
so the correct code is:
$.ajax({
url: url,
type: 'POST',
ContentType: 'application/json',
data: {'data': data}
}).done(function(response){
alert('success');
}).fail(function(jqXHR, textStatus, errorThrown){
alert('FAILED! ERROR: ' + errorThrown);
});
and just FYI in case someone had issues with PHP fetching, this is how to do it:
$data = $this->input->post('data');
$data = json_decode($data);
$sum = $data->sum;
$info_obj = $data->info;
$item_qty = $info_obj[0]->quantity;
send your data as object and not string.. (not sure you have done that already unless we see you data's value.. if not then try it)
data = JSON.stringify(data);
url = base_url + "index.php/home/make_order";
//alert(url);
var request = $.ajax({
url : url,
type : 'POST',
contentType : 'application/json',
data : {data:data} //<------here
});
request.done(function(response){
alert('success');
});
request.fail(function(jqXHR, textStatus, errorThrown){
alert('FAILED! ERROR: ' + errorThrown);
});
updated
if as of comments you data is
{"sum":"2.250","info":[{"id":"6","name":"bla","price":"1.000"}]}
then data:data is fine
var request = $.ajax({
url : url,
type : 'POST',
contentType : 'application/json',
data : data
});
bt you need to change your codeigniter codes to
$this->input->post('sum') // = 2.250
$this->input->post('info')
contentType should be capitalized (ContentType)
see this question
I have extended the CI_Input class to allow using json.
Place this in application/core/MY_input.php and you can use $this->input->post() as usually.
class MY_Input extends CI_Input {
public function __construct() {
parent::__construct();
}
public function post($index = NULL, $xss_clean = NULL){
if($xss_clean){
$json = json_decode($this->security->xss_clean($this->raw_input_stream), true);
} else {
$json = json_decode($this->raw_input_stream, true);
}
if($json){
if($index){
return $json[$index] ?? NULL;
}
return $json;
} else {
return parent::post($index, $xss_clean);
}
}
}
If you are using PHP5.x. Replace
return $json[$index] ?? NULL;
with
return isset($json[$index]) ? $json[$index] : NULL;
I'm not familiar with CodeIgniter, but I think you can try with global variable $_POST:
var_dump($_POST['data']);
If var_dump show data, may be $this->input... has problem

Categories

Resources