Skip to content

Commit

Permalink
Start hooking up the server side
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Jan 15, 2012
1 parent c942e92 commit de1e46e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
6 changes: 6 additions & 0 deletions jquery-attach.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@
get "/" do
haml :index
end

post "/upload" do
name = env["HTTP_X_FILE_NAME"]
file = request.body
data = file.read
end
21 changes: 12 additions & 9 deletions public/javascript/jquery.attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
return this._file;
};

/*
* Get the file name from the Attach.Reader.
*
* reader.fileName();
*/
Attach.Reader.prototype.fileName = function() {
return this.file().name;
};

/*
* Read the file from the user into the browser and depending on
* the events, can error or send to the server.
Expand All @@ -105,8 +114,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* Attach.readError(event);
*/
Attach.readError = function(event) {
console.log("Read file error!");
console.log(event.target.error.name);
};

/*
Expand All @@ -117,7 +124,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Attach.readProgress = function(event) {
if (event.lengthComputable) {
var percentage = event.loaded / event.total;
console.log(percentage);
}
};

Expand All @@ -128,7 +134,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
Attach.readSuccess = function(event) {
var uploader = new Attach.Uploader(event.target);
console.log("Reading file success!");
uploader.send();
};

Expand Down Expand Up @@ -166,14 +171,15 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* uploader.prepareRequest(request);
*/
Attach.Uploader.prototype.prepareRequest = function(request, file) {
Attach.Uploader.prototype.prepareRequest = function(request) {
var form = $("form");
var method = form.attr("method");
var url = form.attr("action");
var reader = Attach.readers.pop();
request.open(method, url);
request.setRequestHeader("Cache-Control", "no-cache");
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
request.setRequestHeader("X-File-Name", this.file().name);
request.setRequestHeader("X-File-Name", reader.fileName());
};

/*
Expand All @@ -195,7 +201,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* Attach.sendError(event);
*/
Attach.sendError = function(event) {
console.log("Send file error!");
};

/*
Expand All @@ -206,7 +211,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Attach.sendProgress = function(event) {
if (event.lengthComputable) {
var percentage = event.loaded / event.total;
console.log(percentage);
}
};

Expand All @@ -216,7 +220,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* Attach.sendSuccess(event);
*/
Attach.sendSuccess = function(event) {
console.log("Sending file success!");
};

})(jQuery);
2 changes: 1 addition & 1 deletion views/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

%body
%form#example(name="example" enctype="multipart/form-data" method="post" action="/upload")
%input#upload(type="file" name="upload" multiple="true")
%input#upload(type="file" name="upload")
%input(type="submit")

0 comments on commit de1e46e

Please sign in to comment.