Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7813433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:51:43+00:00 2026-06-02T04:51:43+00:00

I am using Google Web Toolkit for a project and would like the user

  • 0

I am using Google Web Toolkit for a project and would like the user to select a text file to open in a text window inside the browser. Here’s the almost working code:

 private DialogBox createUploadBox() {
     final DialogBox uploadBox = new DialogBox();
     VerticalPanel vpanel = new VerticalPanel();
     String title = "Select a .gms file to open:";
     final FileUpload upload = new FileUpload();
     uploadBox.setText(title);
     uploadBox.setWidget(vpanel);
     HorizontalPanel buttons = new HorizontalPanel();
     HorizontalPanel errorPane = new HorizontalPanel();
     Button openButton = new Button( "Open", new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            String filename = upload.getFilename();
            int len = filename.length();
            if (len < 5) {
                Window.alert("Please enter a valid filename.\n\tFormat: <filename>.gms");
            } else if (!filename.substring(len-4).toLowerCase().equals(".gms")) {
                Window.alert(filename.substring(len-4) + " is not valid.\n\tOnly files of type .gms are allowed.");
            } else {
                Window.alert(getFileText(filename));
            }
        }
        private native String getFileText(String filename) /*-{
            // Check for the various File API support.
            if (window.File && window.FileReader && window.FileList && window.Blob) {
                // Great success! All the File APIs are supported.
                var reader = new FileReader();
                var file = File(filename);
                str = reader.readAsText(file);
                return str;
            } else {
                alert('The File APIs are not fully supported in this browser.');
                return;
            }
        }-*/;
     });
     Button cancelButton = new Button( "Cancel", 
             new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            uploadBox.hide();               
        }
     });
     buttons.add(openButton);
     buttons.add(cancelButton);
     vpanel.add(upload);
     vpanel.add(buttons);
     vpanel.add(errorPane);
     uploadBox.setAnimationEnabled(true);
     uploadBox.setGlassEnabled(true);
     uploadBox.center();
     return uploadBox;
 }

Whenever I try to actually use this function in my program, I get:

(NS_ERROR_DOM_SECURITY_ERR): Security error

I’m certain it is being cased by:

var file = new File(filename, null);

Disclaimer: I’m not a Javascript programmer by any stretch, please feel free to point out any obvious mistakes I’m making here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:51:45+00:00Added an answer on June 2, 2026 at 4:51 am

    Instead of using window, you should almost always use $wnd. See https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI#writing for more details about JSNI.

    It could also be worthwhile to add a debugger statement while using something like Firebug, or Chrome’s Inspector. This statement will stop the JS code in the debugger as if you had put a breakpoint there, are allow you to debug in Javascript, stepping one line at a time to see exactly what went wrong.

    And finally, are you sure the file you are reading is permitted by the browser? From http://dev.w3.org/2006/webapi/FileAPI/#dfn-SecurityError, that error could be occurring because the browser has not been permitted access to the file. Instead of passing in the String, you might pass in the <input type='file' /> the user is interacting with, and get the file they selected from there.


    Update (sorry for the delay, apparently the earlier update I made got thrown away, took me a bit to rewrite it):

    Couple of bad assumptions being made in the original code. Most of my reading is from http://www.html5rocks.com/en/tutorials/file/dndfiles/, plus a bit of experimenting.

    • First, that you can get a real path from the <input type='file' /> field, coupled with
    • that you can read arbitrary files from the user file system just by path, and finally
    • that the FileReader API is synchronous.

    For security reasons, most browsers do not give real paths when you read the filename – check the string you get from upload.getFilename() in several browsers to see what it gives – not enough to load the file. Second issue is also a security thing – very little good can come of allowing reading from the filesystem just using a string to specify the file to read.

    For these first two reasons, you instead need to ask the input for the files it is working on. Browsers that support the FileReader API allow access to this by reading the files property of the input element. Two easy ways to get this – working with the NativeElement.getEventTarget() in jsni, or just working with FileUpload.getElement(). Keep in mind that this files property holds multiple items by default, so in a case like yours, just read the zeroth element.

    private native void loadContents(NativeEvent evt) /*-{
        if ($wnd.File && $wnd.FileReader && $wnd.FileList && $wnd.Blob) {
            // Great success! All the File APIs are supported.
            var reader = new FileReader();
            reader.readAsText(evt.target.files[0]);
    //...
    

    or

    private native void loadContents(Element elt) /*-{
        if ($wnd.File && $wnd.FileReader && $wnd.FileList && $wnd.Blob) {
            // Great success! All the File APIs are supported.
            var reader = new FileReader();
            reader.readAsText(elt.files[0]);
    //...
    

    For the final piece, the FileReader api is asynchronous – you don’t get the full contents of the file right away, but need to wait until the onloadend callback is invoked (again, from http://www.html5rocks.com/en/tutorials/file/dndfiles/). These files can be big enough that you wouldn’t want the app to block while it reads, so apparently the spec assumes this as the default.

    This is why I ended up making a new void loadContents methods, instead of keeping the code in your onClick method – this method is invoked when the field’s ChangeEvent goes off, to start reading in the file, though this could be written some other way.

    // fields to hold current state
    private String fileName;
    private String contents;
    public void setContents(String contents) {
      this.contents = contents;
    }
    
    // helper method to read contents asynchronously 
    private native void loadContents(NativeEvent evt) /*-{;
        if ($wnd.File && $wnd.FileReader && $wnd.FileList && $wnd.Blob) {
            var that = this;
            // Great success! All the File APIs are supported.
            var reader = new FileReader();
            reader.readAsText(evt.target.files[0]);
            reader.onloadend = function(event) {
                that.@com.sencha.gxt.examples.test.client.Test::setContents(Ljava/lang/String;)(event.target.result);
            };
        } else {
            $wnd.alert('The File APIs are not fully supported in this browser.');
        }
    }-*/;
    
    // original createUploadBox
    private DialogBox createUploadBox() {
      final DialogBox uploadBox = new DialogBox();
      VerticalPanel vpanel = new VerticalPanel();
      String title = "Select a .gms file to open:";
      final FileUpload upload = new FileUpload();
      upload.addChangeHandler(new ChangeHandler() {
        @Override
        public void onChange(ChangeEvent event) {
          loadContents(event.getNativeEvent());
          fileName = upload.getFilename();
        }
      });
      // continue setup
    

    The ‘Ok’ button then reads from the fields. It would probably be wise to check that contents is non null in the ClickHandler, and perhaps even null it out when the FileUpload‘s ChangeEvent goes off.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a xml web service which I would like to track using Google
I'm trying to create a user interface using Google Web Toolkit v2.4. For a
I'm using Google web toolkit for some project mapping seismic activity of some land
I am considering solutions for drawing diagrams using Google Web Toolkit (GWT). Up until
I have a layout in Google Web Toolkit using UIBinder involving a TabLayoutPanel .
I am using Google Web Toolkit's Swing Designer in Eclipse to create and edit
I'm using Google Web Toolkit (GWT) to implement a complex application on the web.
I'm using the Google App Engine in combination with the Google Web Toolkit to
I've run through the Google Web Toolkit StockWatcher Tutorial using Eclipse and the Google
I'm using the Google Web Toolkit (GWT) 2.1. I have a GWT Grid (which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.