English 中文(简体)
ajax和php
原标题:image upload with ajax and php
  • 时间:2009-10-13 08:14:12
  •  标签:

I want to use ajax to upload image. In this module:

  1. On clicking browse and selecting image, it will be uploaded and displayed over file field.
  2. After adding title and description, and clicking on the button, that image will be displayed below and upper image field will be blank
问题回答

你可以通过非洲宇宙航空研究开发机构卸载档案。 你们需要与国际宇宙航行科学院或星基高载体合作。

实际上,你可以在幼.晚版本的east子里用麻.功能上载图像。

传真:

<form action="/" enctype="multipart/form-data" method="post" accept-charset="utf-8">
     <input type="file" name="image"/>
     <button type="submit">
</form>

JS:

$("form").submit(function(){
    var formData = new FormData($(this)[0]);
    $.ajax({
       url: window.location.pathname,
       type:  POST ,
       data: formData,
       async: false,
       cache: false,
       contentType: false,
       processData: false,
       success: function (data) {
              alert(data);
       }
    }); 
    return false;
}); 

该书将通过Ajax向上页发送一份附有生成档案数据的员额申请。 你可以通过改变卢尔参数来改变目的地。

利用J Query plugin重载图像。

可在上查阅。 http://www.uploadification.com/

这将使人们了解如何做到这一点。

假设你在服务器方面有工作能力,这里是一种小型功能,也是如何用java字母实施iframe ha的例子。

<><><><>>>>>

<form name="image-upload">
<input type="file" name="image" /></br>
<button type="submit" name="upload">Upload</button>
<div id="upload-results"></div>
</form>

http://www.ohchr.org。

var fileUpload = function(form /* HTMLElement */, action /* Form Action URL */, callback /* Callback function */) {
    /* vars */
    var atribs = {
        "target": "upload_iframe",
        "action": action,
        "method": "post",
        "enctype": "multipart/form-data",
        "encoding": "multipart/form-data"
    }, iframe;
    /* iframe listener */
    var ilistener = function() {
        var results;
        listener.remove(this,  load , ilistener);
        if(  contentDocument  in this ) {
            results = this.contentDocument.body.innerHTML;
        } else if (  contentWindow  in this ) {
            results = this.contentWindow.document.body.innerHTML;
        } else if (  document  in this ) {
            results = this.document.body.innerHTML;
        } else {
            throw "i m dead jim :/";
        }
        callback.apply(this,[results]); // call the callback, passing the results
        this.parentNode.removeChild(this); // remove the iframe
    };

    /* create the iframe */
    form.parentNode.appendChild(FragBuilder([{"tagName": "iframe","id": "upload_iframe","name": "upload_iframe","style": {"height": "0","width": "0","border": "0"}}]));
    /* collect the iframe back */
    iframe = By.id( upload_iframe );
    /* set the form properties */
    for( var attr in atribs ) {
        if( attr in form ) {
            form[attr] = atribs[attr];
        }
    }
    /* attach the event listener to the iframe */
    listener.add(iframe,  load , ilistener);
    /* submitting the form */
    form.submit();
};

// get the form, and the results area
var form = document.forms[ image-upload ], results = By.id( upload-results );
// listen for the form submit, capture it, and run the iframe upload.
listener.add(form,  submit , function(e) {
    e.preventDefault();
    results.innerHTML =  Uploading... ;
    fileUpload(this,  server.php  /* really anything */, function(data) { 
        console.log(data);
            results.innerHTML = "Uploaded!";
    });
});

Please note: for simplicity purposes I have used the following utilities. https://github.com/rlemon/FragBuilder.js DocumentFragment builder from JSON input.
https://gist.github.com/2172100 event listener, and By utility functions.
*these are both easily removed.





相关问题
热门标签