Until recently I started to realize the possibilities of the html5 file api. How sweet would it be if people could just drag n drop a template from their local filesystem on a drop spot of iscriptesign? Yesterday I started working on the project and now it's finished.
One of the real challenges was to incorporate it in jquery. Okay not really necessary of course but having the greater part of my website in jquery does make it somewhat desirable and you overtyping an example is not challenging is it?
help from stackoverflow and javaranch gave me head start (now that is challenging).
code
jQuery.fn.initDrop = function() {
return this.each(function () {
$(this).bind("dragover", function () {$(this).addClass('focus');$(".drop_container").addClass('focus'); return false;});
$(this).bind("dragleave",function () { $(this).removeClass('focus');$(".drop_container").removeClass('focus'); return false;});
$(this).bind("drop",function(e) {
$(".focus").removeClass("focus");
$(".drop_container").addClass('focus');
e.preventDefault();
var file = e.originalEvent.dataTransfer.files[0];
var reader = new FileReader();
reader.onload = function (evt) {
digestRaw(evt.target.result, true);
};
reader.readAsText(file);
});
$(this).prepend('');
});
};
The div label dropspot is a simple div tag.
<div class="dropspot" id="dropspot">Drop Spot</div>
Which gets its drop behavior by:
$("#dropspot").initDrop();
Lessons
- Unfortunately I only found this working for Firefox (Mac and Windows) and Chrome (Mac, Windows not tested). Safari is not supporting this yet.
- dropevent needs to be unwrapped by retrieving e.orginalevent.
- dragend is not working should be dragleave.
- In my case i need to remove linefeed/carriage return characters for windows(msdos) files I need to do a text.replace("result.replace(/\r\n?|\n/g, "");
3 opmerkingen:
Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something.
I think that you can do with some pics to drive the message home a little bit, but other than that, this is fantastic blog.
A great read. I will certainly be back.
my web-site :: ways to make money online from home
If you would like to get much from this article then you have to apply these methods to your won website.
My web site: How to make extra money at home
electronic cigarette, electronic cigarettes reviews, electronic cigarette, electronic cigarette starter kit, e cigarette, smokeless cigarettes
Een reactie posten