Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 6:58 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: upload file in ajax
PostPosted: Sun Nov 30, 2008 9:31 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2272
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

This 2 paces of code are you to upload files using AJAX, there is an HTML part and JAVASCRIPT part ,now , and with this code , you can upload files without refresh

here the index.php file


Code:
<html>
<head>
     
    <title>Upload File In Ajax</title>
    <script type="text/javascript" src="myscript.js"></script>
    <script type="text/javascript">
       
    </script>
</head>

<body>

    <form action="index.php" method="post" onsubmit="return MSI.submit(this, {'onStart' : startCallback, 'onComplete' : completeCallback})">
        <div>File Name: <input type="text" name="form[name]" /></div>
        <div>File Location: <input type="file" name="form[file]" /></div>
        <div><input type="submit" value="SUBMIT" /></div>
    </form>

    <hr/>

    <div>Times of submit: <span id="numsub">0</span></div>
    <div>Last submit result  (generated by form action - index.php file): <pre id="last"></pre></div>

</body>
</html>


and here the JAVASCRIPT myscript.js file ,
Code:
function startCallback()
{
            // make something useful before submit (onStart)
            return true;
}

function completeCallback(response)
{
            // make something useful after (onComplete)
            document.getElementById('numsub').innerHTML = parseInt(document.getElementById('numsub').innerHTML) + 1;
            document.getElementById('last').innerHTML = response;
}
      
MSI = {

    frame : function(c) {

        var n = 'f' + Math.floor(Math.random() * 99999);
        var d = document.createElement('DIV');
        d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="MSI.loaded(\''+n+'\')"></iframe>';
        document.body.appendChild(d);

        var i = document.getElementById(n);
        if (c && typeof(c.onComplete) == 'function') {
            i.onComplete = c.onComplete;
        }

        return n;
    },

    form : function(f, name) {
        f.setAttribute('target', name);
    },

    submit : function(f, c) {
        MSI.form(f, MSI.frame(c));
        if (c && typeof(c.onStart) == 'function') {
            return c.onStart();
        } else {
            return true;
        }
    },

    loaded : function(id) {
        var i = document.getElementById(id);
        if (i.contentDocument) {
            var d = i.contentDocument;
        } else if (i.contentWindow) {
            var d = i.contentWindow.document;
        } else {
            var d = window.frames[id].document;
        }
        if (d.location.href == "about:blank") {
            return;
        }

        if (typeof(i.onComplete) == 'function') {
            i.onComplete(d.body.innerHTML);
        }
    }

}


I wish it is helpful,

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 using ajax with asp
 Image Insert in Excel File Using POI
 ASP file structure
 upload and download images in my jsp page.
 Search records from text file
 error closing file matlab save
 How to download any file in jsp.
 AJAX SYNTAX ERROR IE
 read php file in asp and display it as html
 javascript read file

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team