2012-08-28 29 views
0

即時上傳文件到服務器併發送參數。但我有兩個問題。如何使用ExtJs將參數傳送到服務器?

1無法發送參數。我做的:

 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(); 
     } 
    }) 
} 

和服務器端:

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(); 
} 
} 

但在控制檯中只得到st=null

2不能使用組合框在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(); 

如果我這樣做在服務器端沒有什麼工作。例如,如果我上傳不是一個zip壓縮文件,我希望得到警報,用它的非zip文件的文字,但沒有得到它。如果我不在面板上添加組合框,我會收到此警報。怎麼了?

+0

什麼版本的ExtJs? – Justin

回答

1

這是因爲請求是多部分請求。然後,你不能讀取參數: String st = request.getParameter(「file_type」);

因爲它會一直爲空。相反,使用以下snipet:

List items = upload.parseRequest(request); 
Iterator iter = items.iterator(); 
while (iter.hasNext()) { 
    FileItem item = (FileItem) iter.next(); 

    if (item.isFormField()) { 
     processFormField(item); 
    } else { 
     processUploadedFile(item); 
    } 
} 

關於你的第二個問題,我無法理解它。

+0

感謝它的工作。第二個問題就是LifeRay的bug。 –

相關問題