open file selection from a link

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonatan

    #1

    open file selection from a link

    Hi all,

    I'm trying to upload files doing something like gmail. I almost got it
    but I have a problem when I post the form.

    Basicaly, I am doing the following.

    When the user presses the link I create a input type="file". Then I
    fire the click event of the control and the open file dialog appears.
    The user selects a file and then the onpropertychang e of the control
    calls the funtion "onFileSelected " where I get the file name. Then I
    create one control to hold the file name and a delete hyperlink to
    delete the controls.

    That part work just fine.

    My problem comes when the user presses the "upload" button. For some
    reason instead of submiting the form it executes de "onFileSelected "
    function again, sets the focus in the textbox next to the "Browse"
    button and removes the name of the file that the user had already
    selected.

    If instead of doing that, I remove the line that hids the input file
    and the line that fires the click event and force the user to press
    the "Browse" button, everything works perfectly. The Upload button
    sends the request to the server where I upload the files!

    Could somebody tell me the difference between firing the click event
    programatically and pressing on the "Browse" button please? Could you
    also let me know how to solve it please?

    This is my code.

    <HTML>
    <HEAD>
    <script language="javas cript">
    function anotherFile(lnk ) {
    if (document.creat eElement &&
    lnk.insertBefor e &&
    lnk.parentNode) {

    var finp = document.create Element('input' );
    var brk = document.create Element('br');

    if (finp && brk) {
    finp.type = 'file';
    finp.name = 'userfile' + (new Date()).getTime () + Math.floor(100
    * Math.random());

    finp.onproperty change = function () {
    onFileSelected( this, brk);
    return false;
    }

    finp.style.visi bility="hidden" ;

    lnk.parentNode. insertBefore(fi np, lnk);
    lnk.parentNode. insertBefore(br k, lnk);

    finp.click();
    }
    }
    return false;
    }

    function onRemove(aLink, aFile, aBr, aS){

    aLink.removeNod e(true);
    aFile.removeNod e(true);
    aBr.removeNode( true);
    aS.removeNode(t rue);

    return false;
    }

    function onFileSelected( finp, brk) {
    if (document.creat eElement &&
    finp.insertBefo re &&
    finp.parentNode ) {

    var a = document.create Element("a");
    var s = document.create Element("span") ;

    if (s && a) {

    a.href = "#";

    a.onclick = function () {
    onRemove(this, finp, brk, s);
    return false;
    }

    var t = document.create TextNode("[Remove]");
    if (t) {
    a.appendChild(t );
    }

    var t2 = document.create TextNode(finp.v alue + " ");
    if (t2) {
    s.appendChild(t 2);
    }

    finp.parentNode .insertBefore(s , finp);
    finp.parentNode .insertBefore(a , finp);
    }
    }

    return false;
    }

    </script>
    </HEAD>
    <body>
    <form id="Form1" method="post" encType="multip art/form-data"
    runat="server">
    <table>
    <tr>
    <td><A id="link2" style="Z-INDEX: 109; WIDTH: 240px; HEIGHT:25px;
    BACKGROUND-COLOR: white" href="#" onclick="return
    anotherFile(thi s);">Another Attachment</A>
    </td>
    </tr>
    <tr>
    <td><input class="button" type="submit" name="upload" value="Upload
    ">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>

    Thank you very much in advance

    Jonatan
  • Sam

    #2
    Re: open file selection from a link

    Hello,


    Did you find any solution about 'upload file programmaticall y' when
    you post the form? I have the same problem :(


    Help much appreciated,
    Thanks in advance,

    Sam

    Jonatan wrote:[color=blue]
    > Hi all,
    >
    > I'm trying to upload files doing something like gmail. I almost got[/color]
    it[color=blue]
    > but I have a problem when I post the form.
    >
    > Basicaly, I am doing the following.
    >
    > When the user presses the link I create a input type="file". Then I
    > fire the click event of the control and the open file dialog appears.
    > The user selects a file and then the onpropertychang e of the control
    > calls the funtion "onFileSelected " where I get the file name. Then I
    > create one control to hold the file name and a delete hyperlink to
    > delete the controls.
    >
    > That part work just fine.
    >
    > My problem comes when the user presses the "upload" button. For some
    > reason instead of submiting the form it executes de "onFileSelected "
    > function again, sets the focus in the textbox next to the "Browse"
    > button and removes the name of the file that the user had already
    > selected.
    >
    > If instead of doing that, I remove the line that hids the input file
    > and the line that fires the click event and force the user to press
    > the "Browse" button, everything works perfectly. The Upload button
    > sends the request to the server where I upload the files!
    >
    > Could somebody tell me the difference between firing the click event
    > programatically and pressing on the "Browse" button please? Could you
    > also let me know how to solve it please?
    >
    > This is my code.
    >
    > <HTML>
    > <HEAD>
    > <script language="javas cript">
    > function anotherFile(lnk ) {
    > if (document.creat eElement &&
    > lnk.insertBefor e &&
    > lnk.parentNode) {
    >
    > var finp = document.create Element('input' );
    > var brk = document.create Element('br');
    >
    > if (finp && brk) {
    > finp.type = 'file';
    > finp.name = 'userfile' + (new Date()).getTime () + Math.floor(100
    > * Math.random());
    >
    > finp.onproperty change = function () {
    > onFileSelected( this, brk);
    > return false;
    > }
    >
    > finp.style.visi bility="hidden" ;
    >
    > lnk.parentNode. insertBefore(fi np, lnk);
    > lnk.parentNode. insertBefore(br k, lnk);
    >
    > finp.click();
    > }
    > }
    > return false;
    > }
    >
    > function onRemove(aLink, aFile, aBr, aS){
    >
    > aLink.removeNod e(true);
    > aFile.removeNod e(true);
    > aBr.removeNode( true);
    > aS.removeNode(t rue);
    >
    > return false;
    > }
    >
    > function onFileSelected( finp, brk) {
    > if (document.creat eElement &&
    > finp.insertBefo re &&
    > finp.parentNode ) {
    >
    > var a = document.create Element("a");
    > var s = document.create Element("span") ;
    >
    > if (s && a) {
    >
    > a.href = "#";
    >
    > a.onclick = function () {
    > onRemove(this, finp, brk, s);
    > return false;
    > }
    >
    > var t = document.create TextNode("[Remove]");
    > if (t) {
    > a.appendChild(t );
    > }
    >
    > var t2 = document.create TextNode(finp.v alue + " ");
    > if (t2) {
    > s.appendChild(t 2);
    > }
    >
    > finp.parentNode .insertBefore(s , finp);
    > finp.parentNode .insertBefore(a , finp);
    > }
    > }
    >
    > return false;
    > }
    >
    > </script>
    > </HEAD>
    > <body>
    > <form id="Form1" method="post" encType="multip art/form-data"
    > runat="server">
    > <table>
    > <tr>
    > <td><A id="link2" style="Z-INDEX: 109; WIDTH: 240px; HEIGHT:25px;[/color]
    [color=blue]
    > BACKGROUND-COLOR: white" href="#" onclick="return
    > anotherFile(thi s);">Another Attachment</A>
    > </td>
    > </tr>
    > <tr>
    > <td><input class="button" type="submit" name="upload"[/color]
    value="Upload[color=blue]
    > ">
    > </td>
    > </tr>
    > </table>
    > </form>
    > </body>
    > </HTML>
    >
    > Thank you very much in advance
    >
    > Jonatan[/color]

    Comment

    Working...