0

Currently I am making a program in which works as a text editor. I have a section where you can rename the title, and when you save, it takes that document.getElementById('title').innerHTML and makes it the .txt name.

For example:

If the title div had the innerHTML of "Document", the file would be called "Document.txt".

My problem is that I want it to also upload a file and take its file name.

For example:

I uploaded a file called "Document.txt", and the function makes the title "Document". (I am using a .replace('.txt','') to make it not show in the title and a += '.txt' to make the file actually a .txt when saved.

Anyone know how to make the file name of the uploaded document using an <input type='file'> a string to be used elsewhere?

Here is some code to put it in perspective:

var id = function(id){return document.getElementById(id)};

function readText(that)
    {
        var reader = new FileReader();
        reader.onload = function(e)
            {  
                var output = e.target.result,
                    name = FILENAMEFIND();
                id('page').innerHTML = output;
                id('title').innerHTML = name;
            }
        reader.readAsText(that.files[0]);
    }
Finley Sherwood
  • 303
  • 4
  • 11

1 Answers1

0

Ok, I figured it out myself:

var id = function(id){return document.getElementById(id)},
    input = id("upfile"),
    file = input.value.split("\\"),
    fileName = file[file.length-1];   // Take purely the file name
    id('title').innerHTML = fileName; // Outputted to title div
Finley Sherwood
  • 303
  • 4
  • 11