Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Automatic upload using javascript

  1. #1
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default Automatic upload using javascript

    The question was presented to my mind, y cant you use ajax to upload files and I got the answer no. However now Ive seen it done in pages.google.com. (Well maybe not using ajax but atleast some javascript magic) Originally I thought ok maybe it could be done by creating a form element dynamically and then attaching a file input element to it with value = to the file that user selects in a similar element rendered on screen.

    While you can get this value from a file input, it seems you cant set it, as an example you cannot do this:

    Code:
    <html>
    <head>
      <script type="text/javascript">
    
        window.onload = function() {
          var upload_input = document.getElementById('upload_input');
          var upload_form  = document.getElementById('upload_form');
          var upload_form_file = document.getElementById('upload_form_file');
    
          upload_input.onchange = function(){
    
            // echo file selected
            alert(this.value);
           
            // echo null
            alert(upload_form_file.value);
    
           // does not work!
            upload_form_file.value = this.value;
    
    
          }
    
    
        }
      </script>
    </head>
    
    <body>
    
    <form>
      <input type="file" id="upload_input" />
    </form>
    <div>
      <form id='upload_form' action='upload.php'>
        <input type="file" id="upload_form_file" />
      </form>
    </div>
    </body>
    </html>
    something about the value filed of a file input being read only. Any other solutions ppl? Bear in mind that javascript has no access to the file system. Well as far as I know.

  2. #2
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Automatic upload using javascript

    There is two issues here
    1) setting input.value
    Had problems in setting this as well and I was going to checking how to do it... but is this really necessary?
    2) uploading with java functions
    I think I saw the code for this somewhere ... I am checking
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  3. #3
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    for (2)

    I searched and searched and found no java code, soory bout that man.

    for (1)

    I found no way of setting the input.value not even writing it directly like
    <input type="text" value="C:\\Temp.txt">
    does not work



    Well I am still looking into the problem. There is an ActiveX object for reading files CreateObject("Scripting.FileSystemObject"), not sure if it reads in binary, but how to convert the file to the proper encoding and post it :euse_think. Even the XML document ActiveX object can post files but those files are text/xml so if any file is loaded into it then that file must be base64 enctype or other MIME stuff.

    Trying to build something with these functions

    window.document.location.reload();
    window.alert("Enter the file names");
    window.document.write();
    window.document.Form1.submit();

    Do you want to upload all the file in a directory automatically?
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  4. #4
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Possibly other active object are there for uploading files

    Got crap so far without ActiveX
    HTML Code:
    <%@ Page language="c#"  Codebehind="WebForm1.aspx.cs" Inherits="UploadTest.WebForm1" %>
    <HTML>
    	<HEAD>
    		<title>Automatic upload using javascript</title>
    			<script id="script1" language="javascript" >
    			function send_file()
    			{
    				alert(document.Form1.idFile1.value);
    				alert(document.Form1.idFile2.name);
    				document.Form1.idText1.value = "new text value";
    				document.Form1.idText1.focus();
    			}
    			</script>
    	</HEAD>
    	<body>
    		<form id="Form1" method="post" enctype="multipart/form-data" runat="server">
    			<INPUT id="idFile1" type="file" name="nFile1" onchange="send_file();" runat="server">
    			<br>
    			<INPUT id="idText1" type="text" name="nText1" value="text box value" onclick="send_file();" onmouseover="idText1.value ='hi mouse'" runat="server">
    			<script id="script2" language="javascript">
    			document.write("<br>");
    			document.write("<INPUT id=\"idFile2\" type=\"file\" name=\"nFile2\" onchange=\"send_file();\" >");
    			document.write("<br>");
    			document.write("<INPUT id=\"idFile3\" type=\"file\" name=\"nFile3\" value=\"directory\" onchange=\"alert(\'Sending one file\'); Form1.submit();\" >");
    			</script>
    		</form>
    		 
    	</body>
    </HTML>
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  5. #5
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default

    really, alex the javascript sandbox prevents it from doing anything illegal and that is very illegal. If you want to do that write a console application.

  6. #6
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default

    Quote Originally Posted by owen
    really, alex the javascript sandbox prevents it from doing anything illegal and that is very illegal. If you want to do that write a console application.
    True, thats what I thought, but its been done, which means its possible. In any event I dont think Id use it, just wanted to see if I could do it. Typical me!

  7. #7
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    There is a code here to base64 encode in javascript

    http://www.onicos.com/staff/iz/amuse/javascript/expert/

    If ActiveX objects are allowed, then possibly files can be read using one object and then base64 encoded and finally sent with XML doc object

    The other alternative may be to compile a java class and use in with the Virtual Machine

    I am on the look out for any javascript code that could help in this task



    Test base 64 encoding here
    http://www.onicos.com/staff/iz/amuse...rt/base64.html
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  8. #8
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I just read that javascript does not support rendering graphics or reading from text files.

    Also java applets (compiled java classes that derived from the applet class and are ment for browsers) do not have access to the hard drive because the security manager checks the applet and prevents any access. I can't believe this. I could have misinterpreted like maybe it has read access but not write; does anyone know?
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  9. #9
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default

    hey, told you so. its a very bad thing

  10. #10
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default

    Quote Originally Posted by owen
    hey, told you so. its a very bad thing
    Hey I know all of that, thanks, but that does change the fact that google pages manages to do it. Have you ever used google pages or even gmail. If you notice in gmail when you select a file to upload if you wait a while it will tell you the size of the file. Now the only way for it to do that is gain access somehow to the file. Apart form that I get that even when I use my firefox browser so I doubt there is any active-x being done.

Posting Permissions

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