OSDN Git Service

implemented simple file uplaod through websocket
authorhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Sun, 9 Aug 2015 16:52:40 +0000 (01:52 +0900)
committerhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Sun, 9 Aug 2015 16:52:40 +0000 (01:52 +0900)
client/app/components/uploadFileModal/uploadFileModal.controller.js
client/app/components/uploadFileModal/uploadFileModal.html
server/app.js
server/ws/index.js [new file with mode: 0644]

index 2b76acd..e6bcad9 100644 (file)
@@ -1,32 +1,32 @@
 'use strict'
 
 angular.module('zephyrApp')
-.controller('UploadFileModalController', function($scope, $modalInstance, Restangular) {
+.controller('UploadFileModalController', function($scope, $modalInstance, Restangular, socket) {
     // To send multipart request from angular, we should set below
     var baseUpload = Restangular.all('/api/upload').withHttpConfig({transformRequest: angular.identity})    
     $scope.upload = function() {
-        var fd = new FormData()
-        console.log($scope.files)
-        /*
-           angular.forEach($scope.files, function(file) {
-
-           fd.append("dir", $scope.path)
-           fd.append("files", file)
-
-           baseUpload.customPOST(fd, undefined, undefined, { 'Content-Type': undefined })
-           .then(function(status) {
-           console.log('File upload', status)
-           $modalInstance.close()
-           })
-           })
-           */
+        var array = []
+        angular.forEach($scope.uploadFiles, function(file) {
+            var param = {
+                data: file,
+                name: file.name,
+                dir: $scope.path
+            }
+            array.push(param)
+        })
+        socket.emit('upload', array)
     }
 
-    $scope.bindFiles = function(files) {
-        $scope.files= files 
+    $scope.bindUploadFiles = function(files) {
+        $scope.uploadFiles= files 
     }
 
     $scope.close = function() {
         $modalInstance.close()
     }
+
+    socket.on('done', function(msg) {
+        console.log(msg)
+        $modalInstance.close()
+    })
 })
index fd00b15..dcff061 100644 (file)
@@ -4,10 +4,8 @@
 
 <div class="modal-body">
     <div class="container">
-        <div flow-init
-            flow-files-submitted="$flow.upload()">
-            <input type="file" flow-btn />
-        </div>
+        <input type="file"
+               onChange="angular.element(this).scope().bindUploadFiles(this.files)" />
     </div>
 </div>
 
index 9e12a77..b8cfbb4 100644 (file)
@@ -20,10 +20,6 @@ var server = app.listen(config.port, config.ip, function () {
 });
 
 var io = require('socket.io').listen(server)
-
-io.on('connection', function(socket) {
-        console.log("WebSocket was connected")
-    })
-
+require('./ws')(io)
 
 exports = module.exports = app;
diff --git a/server/ws/index.js b/server/ws/index.js
new file mode 100644 (file)
index 0000000..2a6259b
--- /dev/null
@@ -0,0 +1,24 @@
+'use strict'
+
+var fs = require('fs')
+
+module.exports = function(io) {
+    io.on('connection', function(socket) {
+        console.log("websocket was connected")
+        /*
+            array include obj
+            {
+                data: blob,
+                name: filename,
+                dir : directory,
+            }
+        */
+        socket.on('upload', function(array) {
+            array.forEach(function(file) {
+                var path = file.dir+'/'+file.name
+                fs.writeFileSync(path, file.data)
+            })
+            socket.emit('done', 'success')
+        })
+    })
+}