how to do file upload using jquery serialization

前端 未结 8 1533
感动是毒
感动是毒 2020-11-22 16:28

So I have a form and I\'m submitting the form through ajax using jquery serialization function

        serialized = $(Forms).serialize();

        $.ajax({
         


        
8条回答
  •  借酒劲吻你
    2020-11-22 17:14

    hmmmm i think there is much efficient way to make it specially for people want to target all browser and not only FormData supported browser

    the idea to have hidden IFRAME on page and making normal submit for the From inside IFrame example

    most important to make a target of form the hidden iframe ID or name and enctype multipart/form-data to allow accepting photos

    javascript side

    function getFrameByName(name) {
        for (var i = 0; i < frames.length; i++)
            if (frames[i].name == name)
                return frames[i];
    
        return null;
    }
    
    function uploadDone(name) {
        var frame = getFrameByName(name);
        if (frame) {
            ret = frame.document.getElementsByTagName("body")[0].innerHTML;
    
            if (ret.length) {
                var json = JSON.parse(ret);
             // do what ever you want 
            }
        }
    }
    

    server Side Example PHP

    
    

提交回复
热议问题