Total Users Online: 0 üye, 224 guest | Tarih/Saat: 2026-04-17 18:23
 
Ajax ve PHP ile yapılmış dosya upload örneği ;
2011-04-29 23:06 GMT  oklu Alnt
Kaan

User is: Online status   Gender_M
Posts: 2772
Ülke: İstanbul - 34
Meslek: Serbest Meslek
Age: 43

Ajax ve PHP ile yap?lm?? dosya upload ?rne?i ;

:
tml]<html>

<body>

<script>

var url = "post.php";

var binary;

var filename;

var mytext;



function upload() {

        filename = document.getElementById('myfile').value;

        mytext = document.getElementById('mytext').value;

        document.getElementById('ajaxbutton').disabled = true;



        // request local file read permission

        try {

                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

        } catch (e) {

                alert("Permission to read file was denied.");

        }

       

        // open the local file

        var file = Components.classes["@mozilla.org/file/local;1"]

                .createInstance(Components.interfaces.nsILocalFile);

        file.initWithPath( filename );          

        stream = Components.classes["@mozilla.org/network/file-input-stream;1"]

                .createInstance(Components.interfaces.nsIFileInputStream);

        stream.init(file,       0x01, 00004, null);

        var bstream =  Components.classes["@mozilla.org/network/buffered-input-stream;1"]

                .getService();

        bstream.QueryInterface(Components.interfaces.nsIBufferedInputStream);

        bstream.init(stream, 1000);

        bstream.QueryInterface(Components.interfaces.nsIInputStream);

        binary = Components.classes["@mozilla.org/binaryinputstream;1"]

                .createInstance(Components.interfaces.nsIBinaryInputStream);

        binary.setInputStream (stream);



        // start AJAX file upload in 1 second

        window.setTimeout("ajax_upload()", 1000);

}



function ajax_upload() {

        // request more permissions

        try {

                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

        } catch (e) {

                alert("Permission to read file was denied.");

        }



        http_request = false;

        http_request = new XMLHttpRequest();

        if (!http_request) {

                alert('Cannot create XMLHTTP instance');

                return false;

        }



        // prepare the MIME POST data

        var boundaryString = 'capitano';

        var boundary = '--' + boundaryString;

        var requestbody = boundary + '\n'

        + 'Content-Disposition: form-data; name="mytext"' + '\n'

        + '\n'

        + mytext + '\n'

        + '\n'

        + boundary + '\n'

        + 'Content-Disposition: form-data; name="myfile"; filename="'

                + filename + '"' + '\n'

        + 'Content-Type: application/octet-stream' + '\n'

        + '\n'

        + escape(binary.readBytes(binary.available()))

        + '\n'

        + boundary;



        document.getElementById('sizespan').innerHTML =

                "requestbody.length=" + requestbody.length;

       

        // do the AJAX request

        http_request.onreadystatechange = requestdone;

        http_request.open('POST', url, true);

        http_request.setRequestHeader("Content-type", "multipart/form-data; \

                boundary=\"" + boundaryString + "\"");

        http_request.setRequestHeader("Connection", "close");

        http_request.setRequestHeader("Content-length", requestbody.length);

        http_request.send(requestbody);



}



function requestdone() {

        if (http_request.readyState == 4) {

                if (http_request.status == 200) {

                        result = http_request.responseText;

                        document.getElementById('myspan').innerHTML = result;                  

                } else {

                        alert('There was a problem with the request.');

                }

                document.getElementById('ajaxbutton').disabled = false;

        }

}



</script>



<form>

Text: <input type="text" id="mytext" name="mytext" size="40">

<br>

File: <input type="file" id="myfile" name="datafile" size="40"><br>

<input type="button" id="ajaxbutton" value="AJAX IT" onclick="upload();">

</form>



<div id="sizespan"></div>

<hr>

<div id="myspan"></div>



</body>

</html>

post.php
:
hp]<?

print_r($_FILES);

?>

<hr>

<?

print_r($_POST);



$fpath = "/tmp/";



// move (actually just rename) the temporary file to the real name

move_uploaded_file ( $_FILES{myfile}{tmp_name}, $fpath.$_FILES{myfile}{name} );



// convert the uploaded file back to binary



// javascript "escape" does not encode the plus sign "+", but "urldecode"

//      in PHP make a space " ". So replace any "+" in the file with %2B first



$filename = $fpath.$_FILES{myfile}{name};

$handle = fopen($filename, "r");

$contents = fread($handle, filesize($filename));

fclose($handle);



$contents = preg_replace("/\+/", "%2B", $contents);



$handle = fopen($filename, "w");

fwrite($handle, urldecode($contents));

fclose($handle);



?>
Konuyla İlgili Diğer Başlıklar
PFS Android Upgulaması Upload
Php ile Resim ve Dosya indirmek
PHP: JSON: AJAX: Youtube Video Aramaları
PHP: ZipArchive:Zip dosya upload işlemleri
Facebook Like URL data Extract Using jQuery PHP and Ajax
Emlak, Oto Galeri, Rent A Car, ?iir, Edebiyat Script Sipari?lerinizi Verebilirsiniz.
Detaylar ??in: kaan@ntka.org

Seditio 170 ?ndir
Capte M?zik ?ndir
Seditio Toolbar ?ndir

 

Powered by Seditio © 2009-2012 All Rights Reserved