PDA

View Full Version : How to send parametrs to server with ExtJs?



maxkliver
27 Aug 2012, 11:14 PM
Im thy to upload file to server and send parametrs. But i have two problems.
1 Cant send parametr. I do:


handler: function(){
mapinfo="mapinfo";
formp.getForm().submit({
url: url_servlet+'uploadfile',
params: {file_type: mapinfo},
success: function(formp, o) {
alert(o.result.file);
kad_tab.getStore().reload()
zoom_store.load();
}
})
}
And server side:




public class uploadfile extends HttpServlet implements Servlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String st = request.getParameter("file_type");
PrintWriter writer = response.getWriter();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
return;
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> list=null;
String mifpath= "1";
String path = " ";
String mif = " ";
String from = "\\\\";
String to ="/";
String error="";
try{
list = upload.parseRequest(request);
Iterator<FileItem> it = list.iterator();
response.setContentType("text/html");
while ( it.hasNext() )
{

FileItem item = (FileItem) it.next();
File disk = new File("C:/uploaded_files/"+item.getName());

path = disk.toString();
String code = new String(path.substring(path.lastIndexOf("."), path.length()).getBytes("ISO-8859-1"),"utf-8");
if (code.equalsIgnoreCase(".zip"))
{
mifpath=path;
mif = mifpath.replaceAll(from, to);
item.write(disk);
//error=unzip.unpack(mif, "C:/uploaded_files");
}
else
{
error = "????????? ???? ?? ???????? ??????? zip";

}
}
}

catch ( Exception e ) {
log( "Upload Error" , e);
}
System.out.println("st="+st);
writer.println("{success:true, file:'"+error+"'}");
writer.close();
}
}





But in console get only st=null.
2 Cant use combobox in fileuploadpanel:



var x = new Ext.Window({
title:'???????? ?????',
items:[
formp = new Ext.FormPanel({
fileUpload: true,
width: 350,
autoHeight: true,
bodyStyle: 'padding: 10px 10px 10px 10px;',
labelWidth: 70,
defaults: {
anchor: '95%',
allowBlank: false,
msgTarget: 'side'
},
items:[{
xtype:"combo",
fieldLabel:'??? ????? ',
name:"cb_file",
id:"cb_file",
mode:"local",
typeAhead: false,
loadingText: '????????...',
store:new Ext.data.SimpleStore({
fields: ['file_name', 'file_type'],
data : [['*.MIF/MID', 'mif'],['*.GPX', 'gpx']]
}),
forceSelection:true,
emptyText:'???????? ???...',
triggerAction:'all',
valueField:'file_type',
displayField:'file_name',
anchor:'60%'
},{
xtype: 'fileuploadfield',
id: 'filedata',
emptyText: '???????? ???? ??? ????????...',
fieldLabel: '??? ?????',
buttonText: '?????'
}],
buttons: [{
text: '?????????',
handler: function(){
mapinfo="mapinfo";
formp.getForm().submit({
url: url_servlet+'uploadfile',
params: {file_type: mapinfo},

success: function(formp, o) {

alert(o.result.file);


kad_tab.getStore().reload()
zoom_store.load();
}
})
}
}]
})
]
})
x.show();





If i do this on server side nothing work. For example if i upload not a zip archive i gona get alert with words that its not zip file, but not get it. If i not add combobox on panel i get this alert. Whats wrong?

scottmartin
17 Sep 2012, 5:45 AM
I would check your server code. Here is a quick test:

// server


<?php

$result = file_get_contents('php://input');
echo '{"success": "true","result": "'.$result.'" }';

?>


// app.js


Ext.onReady(function(){

var states = new Ext.data.ArrayStore({
fields: ['abbr', 'name'],
data : [
{"abbr":"AL", "name":"Alabama"},
{"abbr":"AK", "name":"Alaska"},
{"abbr":"AZ", "name":"Arizona"}
]
});

var combo = new Ext.form.ComboBox({
fieldLabel: 'Choose State',
store: states,
name: 'state',
queryMode: 'local',
displayField: 'name',
valueField: 'abbr'
});

var form = new Ext.form.FormPanel({
title: 'Contact Info',
width: 300,
bodyPadding: 10,
renderTo: Ext.getBody(),
items: [ combo ]
});

new Ext.Button({
text: 'Submit',
renderTo: Ext.getBody(),
handler: function() {
form.getForm().submit({
url: 'response.php',
params: { param_field: 'param_value'},
success: function(form, o){
console.log(o.result);

}
});
}
});

});


console.


Object
result: "param_field=param_value&state="
success: "true"
__proto__: Object


Scott.