Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Need help for 'upload file' function in SmartGWT.

    I wrote the below code for file upload, but once submit is done it is redirecting to the action url.

    My requirement is, once submit is done page should not forward and should present (redirect) in the same page.

    ---------------------Client code----------------------

    public class MywebApp implements EntryPoint {
    public void onModuleLoad() {
    Canvas page = new Canvas();
    final DynamicForm form = new DynamicForm();
    form.setWidth100();
    form.setNumCols(2);
    final UploadItem upload = new UploadItem();

    upload.setName("upload");
    form.setEncoding(Encoding.MULTIPART);
    form.setMethod(FormMethod.POST);
    form.setAction(GWT.getModuleBaseURL()+"fileupload");
    SubmitItem submit = new SubmitItem();

    submit.addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
    form.setCanSubmit(true);
    }
    });

    form.setItems(upload,submit);

    form.draw();
    page.addChild(form);
    page.draw();

    }

    }




    ------------------------ servlet Code -------------------------------


    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    // process only multipart requests
    if (ServletFileUpload.isMultipartContent(req)) {

    // Create a factory for disk-based file items
    FileItemFactory factory = new DiskFileItemFactory();

    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);

    // Parse the request
    try {
    List<FileItem> items = upload.parseRequest(req);
    for (FileItem item : items) {
    // process only file upload - discard other form item types
    if (item.isFormField())
    continue;

    String fileName = item.getName();
    // get only the file name not whole path
    if (fileName != null) {
    fileName = FilenameUtils.getName(fileName);
    }

    File uploadedFile = new File(UPLOAD_DIRECTORY, fileName);
    if (uploadedFile.createNewFile()) {
    item.write(uploadedFile);
    resp.setStatus(HttpServletResponse.SC_CREATED);
    resp.getWriter().print(
    "The file was created successfully.");
    resp.flushBuffer();
    } else
    throw new IOException(
    "The file already exists in repository.");
    }
    } catch (Exception e) {
    resp.sendError(
    HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
    "An error occurred while creating the file : "
    + e.getMessage());
    }

    } else {
    resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
    "Request contents type is not supported by the servlet.");
    }
    }

    #2
    Hi

    Did u find why the page is getting redirected ? . please let me know if u find the anwer

    Comment


      #3
      I also faced the same issue. I searched several forums, i couldn't find proper executable solution. I got it resolved by taking plain GWT file upload classes instead of taking smartgwt classes and wrap them into SmartGwt layouts for look and feel. Please find the attached ones which are fully functional.
      Hope this helps.
      Attached Files

      Comment


        #4
        Hi,

        Take a look at this post -> http://uptick.com.au/content/taking-advantage-apache-fileupload-and-opencsv

        - Mark

        Comment


          #5
          Please, let me know how to stay at the same page after file loads. Thanks in advance!

          Comment


            #6
            Take a look at the Upload overview here:
            http://www.smartclient.com/smartgwte...cs/Upload.html

            Comment


              #7
              Originally posted by skaluva
              I also faced the same issue. I searched several forums, i couldn't find proper executable solution. I got it resolved by taking plain GWT file upload classes instead of taking smartgwt classes and wrap them into SmartGwt layouts for look and feel. Please find the attached ones which are fully functional.
              Hope this helps.
              I use those files which skaluva uploaded. But I don't know how to adjust "web.xml" to run it. Can anybody help me and give some hint. Thanks a lot.

              Comment

              Working...
              X