Загрузка файла с формой dhtmlx и распорками 2 - PullRequest
0 голосов
/ 01 октября 2019

Я работаю с DHTMLX, когда я загружаю файл, используя форму dhtmlx, но file, fileCotentType и fileFileName становятся нулевыми, а код бэкэнда, который я использую java Struts 2, если я загружаю файл, используя теги jsp struts, этоработает нормально, только проблема в том, что когда я загружаю файл с переменными формы dhtmlx, которые становятся нулевыми, следующий файл - класс действия

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.erwin.action;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class UploaderAction extends ActionSupport implements
        ServletRequestAware {

    private String name;
    private String email;
    private String phone;
    private File file;
    private String fileContentType;
    private String fileFileName;

    private HttpServletRequest servletRequest;

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

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String execute() {
        try {
            String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
            System.out.println("Server path:" + filePath);
            File fileToCreate = new File(filePath, this.fileFileName);

            FileUtils.copyFile(this.file, fileToCreate);
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(e.getMessage());

            return INPUT;
        }
        return SUCCESS;
    }

    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;

    }

    public String fileUploader() {
        return SUCCESS;
    }

    public String fileData() {
        return SUCCESS;
    }
}


, следующий файл - код dhtmlx

declare var dhtmlXLayoutObject: any
declare var dhtmlXWindows: any;
module com.erwin {
  export class Parent {
    myLayout: any;
    myToolbar: any;
    myWins: any;
    window: any;
    myForm: any;
    myGrid: any;

    constructor() {
      this.addLayout();
    }
    public addLayout() {
      this.myLayout = new dhtmlXLayoutObject({
        parent: document.body,
        pattern: "1C",
        cells: [{
          id: "a",
          text: "File Uploader"
        }]
      });
      this.addToolbar();
    }
    public addToolbar() {
      this.myToolbar = this.myLayout.cells("a").attachToolbar({
        icons_path: "./dhtmlx/icons/",
        items: [{
          name: "Upload", id: "upload", type: "button", img: "add.png"
        }]
      });
      this.myToolbar.attachEvent("onclick", (id: any) => {
        if (id == "upload") {
          this.addWindow();
        }
      });
    }
    public addWindow() {
      this.myWins = new dhtmlXWindows();
      this.window = this.myWins.createWindow("add", 500, 100, "400", "300");
      this.window.setText("Upload File");
      this.addForm();
    }
    public addForm() {
      this.formData = [{
        type: "settings", position: "label-left", labelWidth: 80, inputWidth: 120
      },
      { id: "name", type: "input", name: 'name', label: 'Name:   ' },
      { id: "email", type: "input", name: 'email', label: 'Email:   ' },
      { id: "phone", type: "input", name: 'phone', label: 'Phone no:' },

      {
        type: "fieldset", label: "Uploader", list: [
          { type: "upload", name: "file", inputWidth: 330, url: "fileUploader.action", swfPath: "./dhtmlx/codebase/ext/uploader.swf" },
          { type: "button", name: "submit", value: "upload" },
          { type: "container", name: "mycontainer", inputWidth: 330 }
        ]
      }];
      this.myForm = this.window.attachForm();
      this.myForm.load(this.formData);

      this.myForm.attachEvent("onButtonClick", (name: any) => {
        if (name == "submit") {

          //alert("ahkjg");
          this.myForm.send("fileData.action", (response) => {

            console.log(response)
          });
        }
      });
      this.addGrid();
    }
    public addGrid() {
      this.myGrid = this.myLayout.cells("a").attachGrid();
      this.myGrid.setHeader("Name,Email,Phone no,File Name");
      this.myGrid.enableAutoWidth(true);
      this.myGrid.init();
    }
  }
}

Struts. xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>   
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
        <action name="fileUploader" class="com.erwin.action.UploaderAction" method="fileUploader">
            <result>jsp_Files/fileUploader.jsp</result>
        </action>

        <action name="fileData" class="com.erwin.action.UploaderAction" method="execute">
            <interceptor-ref name="fileUpload">
                <param name="maximumSize">2097152</param>
                <param name="allowedTypes">
                    image/png,image/gif,image/jpeg,image/pjpeg
                </param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success">SuccessUserImage.jsp</result>
            <result name="input">UserImage.jsp</result>
        </action>
    </package>
</struts>
...