Skip to content

Commit a9aa9d6

Browse files
author
Pablo Cantero
committed
Adding file to the upload
1 parent 47a57ce commit a9aa9d6

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

examples/progress/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
$('#upload_percent').text(data.percent);
1717
$('#upload_status').text(data.status);
1818
$('#upload_filename').text(data.fileName);
19+
$('#upload_filepath').text(data.filePath);
1920
}).error(function(){clearInterval(intervalID)});
2021
}, 250);
2122
return true;
@@ -31,7 +32,8 @@ <h1>Super Upload</h1>
3132
<input type="file" name="upload" id="upload"><br>
3233
<span id="upload_percent"></span><br/>
3334
<span id="upload_status"></span><br/>
34-
<span id="upload_filename"></span>
35+
<span id="upload_filename"></span><br/>
36+
<span id="upload_filepath"></span>
3537
</p>
3638
<p>
3739
<input type="submit" value="Upload">

lib/node-upload-progress.coffee

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ class UploadHandler
2626
func.call @
2727

2828
formOnFile: (upload, field, file) ->
29-
upload.fileName = file.name
29+
upload.file = file
3030
if @uploadDir
3131
fs.rename file.path, "#{@uploadDir}/#{file.name}"
32-
32+
file.path = "#{@uploadDir}/#{file.name}"
33+
3334
formOnProgress: (upload, bytesReceived, bytesExpected) ->
3435
upload.updateProgress bytesReceived, bytesExpected
3536

lib/node-upload-progress.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
};
2525

2626
UploadHandler.prototype.formOnFile = function(upload, field, file) {
27-
upload.fileName = file.name;
27+
upload.file = file;
2828
if (this.uploadDir) {
29-
return fs.rename(file.path, "" + this.uploadDir + "/" + file.name);
29+
fs.rename(file.path, "" + this.uploadDir + "/" + file.name);
30+
return file.path = "" + this.uploadDir + "/" + file.name;
3031
}
3132
};
3233

lib/upload.coffee

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Upload
2-
constructor: (@fileName='', @bytesReceived=0, @bytesExpected=0) ->
2+
@file = null
3+
4+
constructor: (@bytesReceived=0, @bytesExpected=0) ->
35

46
isDone: ->
57
@bytesReceived == @bytesExpected
@@ -13,7 +15,8 @@ class Upload
1315
bytesExpected: @bytesExpected
1416
percent: @percent()
1517
status: @status()
16-
fileName: @fileName
18+
fileName: @file.name if @file
19+
filePath: @file.path if @file
1720

1821
status: ->
1922
return 'done' if @isDone() and @fileName

lib/upload.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
Upload = (function() {
55

6-
function Upload(fileName, bytesReceived, bytesExpected) {
7-
this.fileName = fileName != null ? fileName : '';
6+
Upload.file = null;
7+
8+
function Upload(bytesReceived, bytesExpected) {
89
this.bytesReceived = bytesReceived != null ? bytesReceived : 0;
910
this.bytesExpected = bytesExpected != null ? bytesExpected : 0;
1011
}
@@ -23,7 +24,8 @@
2324
bytesExpected: this.bytesExpected,
2425
percent: this.percent(),
2526
status: this.status(),
26-
fileName: this.fileName
27+
fileName: this.file ? this.file.name : void 0,
28+
filePath: this.file ? this.file.path : void 0
2729
});
2830
};
2931

test/unit/upload_test.coffee

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ describe 'Upload', ->
1717

1818
it 'should return JSON', ->
1919
$.upload.updateProgress 1, 10
20-
$.upload.toJSON().should.equal '{"bytesReceived":1,"bytesExpected":10,"percent":10,"status":"uploading","fileName":""}'
20+
$.upload.file = name: 'hello.txt', path: "#{__dirname}/hello.txt"
21+
$.upload.toJSON().should.equal '{"bytesReceived":1,"bytesExpected":10,"percent":10,"status":"uploading","fileName":"hello.txt","filePath":"' + __dirname + '/hello.txt"}'
22+
23+
it 'should return JSON without file', ->
24+
$.upload.updateProgress 1, 10
25+
$.upload.toJSON().should.equal '{"bytesReceived":1,"bytesExpected":10,"percent":10,"status":"uploading"}'
2126

2227
describe 'percent', ->
2328
it 'should return 100%', ->

0 commit comments

Comments
 (0)