English 中文(简体)
• 如何用JQuery进行超载档案
原标题:how to Upload file asynchronously using JQuery
  • 时间:2012-05-14 10:00:29
  •  标签:
  • php
  • jquery

我想用 j子立立案,而不使用任何PLUGIN。

JQuery对我来说是很新的,在我研究各种论坛之后,我最后通过了这一法典:


HTML:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">


$(document).ready(function(){

    $( #myform ).submit(function() {

        var fileInput = document.getElementById( file );
        var file = fileInput.files[0];
        var formData = new FormData();
        formData.append( file , file);

        $.ajax({
            type: "POST",
            url: "upload.php",
            data: formData,
            processData: false,



            contentType:  multipart/form-data ,


                beforeSend: function (x) {
                        if (x && x.overrideMimeType) {
                            x.overrideMimeType("multipart/form-data");
                        }
                },

            success:function(msg){
                    //alert( "Data Uploaded: " + msg );
                document.getElementById( display ).innerHTML = msg;

                }
            });

        return false;
    });


});

</script>
</head>

<body>
<form enctype="multipart/form-data" id="myform" name="myform" method="POST">
<input name="file" type="file" id="file" name="file"/>
<input type="text" name="txtValue" value="" id="txtValue">-->
<input type="submit" value="Upload" id="button" name="button"/>

<div id="display"></div>
</form>

</body>
</html>

PHP:

<?php

$uploaddir =  ./uploads/ ;
$file = $uploaddir . basename($_FILES[ file ][ name ]);

if (move_uploaded_file($_FILES[ file ][ tmp_name ], $file)) {
  $value = "success";
} 
else {
    $value = "error";
}


echo $value; 

?>

该守则没有发挥作用,每当“显示”数字四印刷“探测器”。 请帮助我。

问题回答

隐蔽。 部分采用<代码>iframe,并在iframe sid上设定target/code>。

In jQuery. In after document ready function add a load event handler (say LEH)to the iframe. So that when the form is submitted and file is uploaded and iframe is loaded then the LEH will get called

这将像成功事件一样。

注:在装满该页时,你需要做小的拖车,然后装上机体。 因此,还将进行第一次检查。

I have user Jquery Form Plugin in my project. JQuery the raw xhr object is wrapped in jqXhr Object which doesn t have any reference to the new upload property of the xhr. Hope you can start with this below example. html:

 <input type="file" class="text-input" size="50" id="file_upload" value=""  name="file_upload"/>

var formData = new FormData($( form )[0]);
$.ajax({
url:  /files/add_file ,  //server script to process data
type:  POST ,
xhr: function() {  // custom xhr
 myXhr = $.ajaxSettings.xhr();
 if(myXhr.upload){ // check if upload property exists
    //myXhr.upload.addEventListener( progress ,progressHandlingFunction, false); // for handling the progress of the upload
}
return myXhr;
},
dataType:  JSON ,
beforeSend: beforeSendHandler,
success: function(data) {
    if (data.error){
       showMessage(data.html, false, false);
        }
    else{
       showMessage(data.html, false, false);
setTimeout("window.location =  path/to/after/uploading ;",450)
}
},
error: function(data) {
    showMessage(data.html, false, false);
},
// Form data
data: formData,
   //Options to tell JQuery not to process data or worry about content-type
cache: false,
contentType: false,
processData: false
});




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...