Giter Site home page Giter Site logo

Comments (2)

kgust avatar kgust commented on June 13, 2024

@martyphee Did you get this to work? I'm also getting a 405.

from jquery-iframe-transport.

martyphee avatar martyphee commented on June 13, 2024

I don't believe I did.

Looking back at the repo I used this, but don't remember...

/**
 * Created with IntelliJ IDEA.
 * User: mphee
 * Date: 12/8/12
 * Time: 12:00 PM
 * To change this template use File | Settings | File Templates.
 */
/**
 * AJAX File Upload
 * http://github.com/davgothic/AjaxFileUpload
 *
 * Copyright (c) 2010 David Hancock
 * Licensed under the MIT license ( http://davgothic.com/mit-license/ )
 */

(function($) {
    $.fn.ajaxfileupload = function(options) {

        var defaults = {
                    debug: false,
                    action: "upload.php",
                    onChange: function(file){},
                    onSubmit: function(element, file){},
                    onComplete: function(element, file, response){}
                },
                settings = $.extend({}, defaults, options);

        this.each(function() {
            var $this = $(this);
            if ($this.is("input") && $this.attr("type") === "file") {
                log("Applying to file input with id " + $this[0].id);
                $this.bind("change", onChange);
            } else {
                log("Ignoring invalid element");
            }
        });

        function onChange(e) {
            var element = $(e.target),
                    file    = filename(element.val()),
                    iframe  = createIframe(),
                    form    = createForm(iframe);
            if( settings.onChange.call(this, file) === false ) {
                return false;
            }

            var originalForm = element[0].form;

            iframe.bind("load", {element: element, file: file}, onComplete);
            form.append(element).bind("submit", {element: element, file: file, orginalForm: originalForm}, onSubmit).submit();
        }

        function onSubmit(e) {
            // If false cancel the submission
            if (settings.onSubmit.call(e.data.element, e.data.file) === false) {
//                $("span." + e.data.element.attr("id")).replaceWith(e.data.element);
                return false;
            }

            // Clean up
            setTimeout(function() {
                $(this).remove();
                e.data.element.remove();
                e.data.element = null;
            }, 0);
        }

        function onComplete (e) {
            var iframe  = $(e.target),
                    doc     = iframe[0].contentDocument ? iframe[0].contentDocument : window.frames[iframe[0].id].document,
                    response = doc.body.innerHTML;

            if (response) {
                try {
                    response = jQuery.parseJSON(response);
                } catch(e) {
//                    console.log(e);
                    response = jQuery.parseJSON(doc.body.innerText);
                }

            } else {
                response = {};
            }

            // Fire our callback
            settings.onComplete.call(e.data.element, e.data.file, response);

            // Remove the iframe after short delay
            setTimeout(function() {
                iframe.remove();
            }, 0);
        }

        function filename(filePath) {
            return filePath.replace(/.*(\/|\\)/, "");
        }

        var randomId = (function() {
            var id = 0;
            return function () {
                return "_AjaxFileUpload" + id++;
            };
        })();

        function createIframe() {
            var id      = randomId(),
                    iframe  = $("<iframe/>")
                            .attr({
                                      src: "javascript:false;",
                                      name: id,
                                      id: id
                                  })
                            .hide()
                            .appendTo("body");

            return iframe;
        }

        function createForm(iframe) {
            var form = $("<form />")
                    .attr({
                              method: "post",
                              action: settings.action,
                              enctype: "multipart/form-data",
                              target: iframe[0].name
                          })
                    .hide()
                    .appendTo("body");

            return form;
        }

        function log(output) {
            if (settings.debug === true && typeof(console) !== "undefined" && typeof(console.log) === "function") {
                console.log("[AjaxFileUpload] " + output);
            }
        }

        return this;
    }
})(jQuery);

from jquery-iframe-transport.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.