Comments (2)
@martyphee Did you get this to work? I'm also getting a 405.
from jquery-iframe-transport.
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)
- Submitting request with Opera prompts file download. HOT 2
- Fix for object, array and string data support HOT 2
- Script5: Access denied when trying to submit form HOT 22
- [patch] If the textarea exists but does not have data-status and data-statusText attributes, they end up being NULL. HOT 2
- [patch] If the textarea exists but does not have data-status and data-statusText attributes, they end up being NULL.
- When submitting file on Chrome, response is 200 OK but status is "(canceled)".
- Aborting an upload
- File input events HOT 2
- [enhancement] Add missing bower.json. HOT 2
- LICENSE? HOT 1
- Please add version tags HOT 3
- File not chosen after starting upload
- Content Security Policy warning
- IE asks to download JSON file -- Demo HOT 3
- Documentation for Browser Support
- [object Object] and Firefox 35.0 HOT 1
- Not available via npm? HOT 1
- Firefox (49.0.1) issue with ifame upload plugin HOT 12
- How to get an error if the server fails to respond?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jquery-iframe-transport.