www.webdeveloper.com
Results 1 to 7 of 7

Thread: passing data from a file upload

  1. #1
    Join Date
    Jan 2012
    Posts
    14

    passing data from a file upload

    I've been googling for a few hours on this with no luck.

    im wondering if its possible to have a <input type="file"> and pass that data into a variable (and then execute it as code using eval())

    like
    Code:
    mainLevelData=document.getElementById('file1').fileContents;
    or what an alternative would be to achieve this. (it has to be some kind of dialouge to choose file from)

  2. #2
    Join Date
    Dec 2011
    Posts
    22
    as far as I'm aware you wont ever be able to do this as it breaches many security issues, it would be a violation of the sandbox.

  3. #3
    Join Date
    Jan 2012
    Posts
    14
    actually i found something in html5. its very quirky but...

    PHP Code:
    var reader = new FileReader();
    function 
    readText(){
        
    reader.readAsText(document.getElementById("file1").files[0]);
        
    document.getElementById('main').innerHTML=reader.result;

    HTML Code:
    <input id="file1" type="file" onChange='readText()' />
    there's issues with the reader.result. here it works fine, but in other cases reader.result has returned null when called right after the readAsText function. so putting w/e instruction that calls reader.result into a setTimeout("instr", 10); would solve the issue.

    Now i just wonder why it wont work in chrome. I've analyzed stuff and i can see chrome is fully compatible with the FileSystem object and the element.files[0]. I can't figure out why chrome always forget passing the value from readAsText into the reader.result value. reader.result just remains blank. urghh

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    don't use timeouts, use events:

    PHP Code:

    function readText(that){
      if(
    that.files && that.files[0]){
       var 
    reader = new FileReader();
       
    reader.onload = function (e) {  
         
    document.getElementById('main').innerHTMLe.target.result
       };
    //end onload()
       
    reader.readAsText(that.files[0]);
      }
    //end if html5 filelist support

    HTML Code:
    <input type="file" onchange='readText(this)' />
    Last edited by rnd me; 01-02-2012 at 04:28 AM.

  5. #5
    Join Date
    Jan 2012
    Posts
    14
    clever solution, ill use that instead, but it still wont work in chrome:

    http://localhostr.com/files/obJMYCW/readFile.html

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by cyberfox View Post
    clever solution, ill use that instead, but it still wont work in chrome:

    http://localhostr.com/files/obJMYCW/readFile.html
    it works just fine for me. you do know that it must be viewed from an http:// url don't you?

    if your address bar url doesn't begin with http, but rather with file, it won't work.
    when i published the text in your linked file to the web, the demo runs just fine in chrome 15.

  7. #7
    Join Date
    Jan 2012
    Posts
    14
    that explains. so only firefox can run from file:.

    thnx for the help ^^

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles