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

  • SEARCH
  • Home
  • 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 7693173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:01:17+00:00 2026-05-31T21:01:17+00:00

In a prior post I was trying to upload a file to a server

  • 0

In a prior post I was trying to upload a file to a server using HTML and Javascript. I ran into several problems with my implementation so I’ve taken a different approach. I have a HTML form and a python script in the cgi directory of my webserver. Here is my HTML code…

<html>
<head>
<script type="text/javascript">
    function loadXMLDoc(){
        var xmlhttp;
        if (window.XMLHttpRequest){
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else{
            // code for IE6, IE5 seriously, why do I bother?
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("outputDiv").innerHTML=xmlhttp.responseText;
                }
        }
        var file = document.getElementById('idexample').value;
        xmlhttp.open("GET","/cgi/ajax.py?uploadFile="+file,true);
        xmlhttp.send();
    }
</script>
</head>

<body onload="changeInput()">
<form name = "form_input" enctype="multipart/form-data" method="POST">
    <input type="file" ACCEPT="text/html" name="uploadFile" id="idexample" />
    <button type="button" onclick="loadXMLDoc()">Enter</button>
</form>
<div id="outputDiv"></div>
</body>
</html>

I am using AJAX because it occured to me that my cgi script could take up to a couple of minutes to run depending on the file the user enters. What I’m trying to do is get the contents of the file passed to my python CGI script and print them out on the page. All I’m getting is “C:\fakepath\. What I want to do is get the file contents. Here is my cgi script…

#!/usr/bin/python
import cgi

form = cgi.FieldStorage()
print 'Content-Type: text/html\n'
if form.has_key('uploadFile'):
    print str(form['uploadFile'].value)
else:
    print 'no file'

Also should I be using

xmlhttp.open("POST","/cgi/ajax.py",true); 

instead of

xmlhttp.open("GET","/cgi/ajax.py?uploadFile="+file,true);

I tried both but the POST one doesn’t even return the name of my file. Also it occured to me that I read that I may not even need the tags in my script since I submit this information using javascript. Is this true. My page seems to work without the form tags (at least on Chrome and Firefox).

  • 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-05-31T21:01:18+00:00Added an answer on May 31, 2026 at 9:01 pm

    If you examine the variable file using console.log(), you will notice that it only contains the filename and not its contents.

    var file = document.getElementById('idexample').value;
    console.log(file); // Outputs filename
    

    The standard way to upload a file via AJAX is to use an iframe. The following code is taken from jquery.extras.js and is based on malsup’s form plugin.

    <html>
    <body>
    
    <form id=input action=/cgi/ajax.py method=post>
        <input type=file name=uploadFile>
        <input type=submit value=Submit>
    </form>
    
    <div id=output></div>
    
    <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
    <script>
        $.fn.ajaxForm = function(options) {
            options = $.extend({}, {
                onSubmit:function() {},
                onResponse:function(data) {}
            }, options);
            var iframeName = 'ajaxForm', $iframe = $('[name=' + iframeName + ']');
            if (!$iframe.length) {
                $iframe = $('<iframe name=' + iframeName + ' style="display:none">').appendTo('body');
            }
            return $(this).each(function() {
                var $form = $(this);
                $form
                    .prop('target', iframeName)
                    .prop('enctype', 'multipart/form-data')
                    .prop('encoding', 'multipart/form-data')
                    .submit(function(e) {
                        options.onSubmit.apply($form[0]);
                        $iframe.one('load', function() {
                            var iframeText = $iframe.contents().find('body').text();
                            options.onResponse.apply($form[0], [iframeText]);
                        });
                    });
            });
        };
        $('#input').ajaxForm({
            onResponse:function(data) {
                $('#output').html(data);
            }
        });
    </script>
    
    </body>
    </html>
    

    Your CGI code is fine. However, I highly recommend using a web framework like Pyramid or Django for your own sanity.

    #!/usr/bin/python
    import cgi
    
    form = cgi.FieldStorage()
    print 'Content-Type: text/html\n'
    if 'uploadFile' in form and form['uploadFile'].filename:
        print form['uploadFile'].value
    else:
        print 'no file'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to allow multiple file uploads prior to a form post. What
I am trying to execute this SQL query prior to restoring a .BAK file
Trying to get JQuery to post JSON to a server: $.ajax({ url: /path/to/url, type:
I'm trying to split some prior crafted code into a DLL. It's a simple
Prior to Excel 2007, we used to catch the open file event in Excel
I use $_POST to read data sent through HTML forms to the server, narrowing
I read various post's prior to this. but none of them seemed to work
I'm trying to send data from a form to an external script prior to
I'm trying to pass a variable from one include file to another. This is
I have a client submitting a web-request (POST) of XML data to a server.

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.