]> insang Git - ZipProject.git/commitdiff
서버단 파일업로드 기능 추가
authorjoinsang <dhkq92@icloud.com>
Sat, 23 Feb 2019 05:42:24 +0000 (14:42 +0900)
committerjoinsang <dhkq92@icloud.com>
Sat, 23 Feb 2019 05:42:24 +0000 (14:42 +0900)
코드작성후 테스트는 안해봄 안될수도

ZipProject/Controllers/WebZipController.cs
ZipProject/Models/ZipModel.cs
ZipProject/Views/WebZip/partial_zip.cshtml
ZipProject/wwwroot/Language/en.json
ZipProject/wwwroot/Language/kr.json
ZipProject/wwwroot/js/Zip/partial_zip.js

index 84d4b2a3c6d3a592bfcf14616d396f1f77393272..8228ce1128c2e87213e7c8843f94f0e6cbc068c4 100644 (file)
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using ZipProject.Models;
+using Newtonsoft.Json.Linq;
 
 // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
 
@@ -46,6 +47,14 @@ namespace ZipProject.Controllers {
       return Redirect ("/");
     }
 
+    public JsonResult FileUpload(List<string> dirList, List<IFormFile> files, string fileKeyPair){
+      JObject keyPair_FileDir= JObject.Parse(fileKeyPair);
+
+      ZipModel zipModel= new ZipModel();
+      zipModel.SaveFiles(files);
+
+      return Json(keyPair_FileDir["building.html"]);
+    }
   }
 
 }
\ No newline at end of file
index ada818b95959f741b181df32211b2ceaf65115d5..a4faecc800d7677f05509c51ae1f6e0d5848179e 100644 (file)
@@ -1,16 +1,35 @@
-using Ionic.Zip;
+using System.IO;
+using Ionic.Zip;
+using Newtonsoft.Json.Linq;
+using System.Collections.Generic;
+using System;
+using Microsoft.AspNetCore.Http;
 
 namespace ZipProject {
   public class ZipModel {
-
     private readonly string[] _FOTMAT = { "zip", "7z", "rar" };
 
     public ZipModel () { }
 
-    public void MakeZip (string name, string format = "zip") {
+    public string SaveFiles(List<IFormFile> files){
+      var path= Environment.CurrentDirectory + "/wwwroot/zip/";
+      Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
+      path += unixTimestamp+"/";
+
+      foreach(var file in files){
+        if (file.Length > 0){
+          var filepath= path+ file.FileName;
+          using(var fileStream= new FileStream(filepath, FileMode.Create)){
+            file.CopyTo(fileStream);
+          }
+        }
+      }
+
+      return unixTimestamp+"";
+    }
+
+    public void MakeZip (JObject keypair, List<string> dirList, List<IFormFile> files) {
       using (ZipFile zip = new ZipFile ()) {
-        zip.AddDirectory("_zip/test");
-        zip.Save("_zip/" + name + ".zip");
       }
     }
   }
index 69a4cc7005885bc08bf3e906cac6286d28db810f..c017ad33d7c9127b60f66cbabe025cd5771f6d77 100644 (file)
         <label class="btn btn-danger webzip-btn-choosefile" for="btn_newDirectory">\r
           @LanguagePack.GetText(lang, "newFolder")\r
         </label>\r
-        <label class="btn btn-danger webzip-btn-choosefile">\r
+        <label class="btn btn-danger webzip-btn-choosefile" onclick="alert(_Language['comingsoon'])">\r
           @LanguagePack.GetText(lang, "howtouse")\r
         </label>\r
       </td>\r
     </tr>\r
+    <tr>\r
+      <td colspan="2">\r
+        <label class="btn btn-danger webzip-btn-choosefile" onclick="RequestCompress();">\r
+          @LanguagePack.GetText(lang, "compress")\r
+        </label>\r
+      </td>\r
+    </tr>\r
   </table>\r
 </div>\r
 <div class="col-sm-7 webzip-section-sidezip webzip-section-explore" id="zipExplore"\r
index cd409765aa5a2b150a245d2ebf7a44d0db2d2dcc..414767e77dc623a670e1cd53feb02823c0c05c6e 100644 (file)
@@ -13,5 +13,6 @@
   "duplicateName": "It can not have the same name as a file already added:",
   "addDirectory": "Add Directory",
   "howtouse": "How to use",
-  "comingsoon": "Coming soon"
+  "comingsoon": "Coming soon",
+  "compress": "Compress!"
 }
index 1dabed41ee056a76e3303e6c24e7265425be1656..acc1ff71b0b527f90de69023d96456d38b935a11 100644 (file)
@@ -13,5 +13,6 @@
   "duplicateName": "이미 추가되어있는 파일과 이름이 같을수 없습니다 : ",
   "addDirectory": "폴더추가",
   "howtouse": "사용법",
-  "comingsoon": "준비중입니다."
+  "comingsoon": "준비중입니다.",
+  "compress": "압축시작!"
 }
index b91114ddc234b114f08715300c7ccea70fa3c89f..4c20fcf71d58b317ef1f262d646030e7445ea0a1 100644 (file)
@@ -35,7 +35,7 @@ function AddFolder() {
   selected.append(directory);
   //directories 객체에 폴더 오브젝트 추가
   directories.put(directories.keys().length + "", "new");
-  
+
   //bullet 리셋, sort리셋
   SetBulletIcon();
   SetSortable();
@@ -109,7 +109,6 @@ function SetSortable() {
             );
           });
 
-        console.log(directory_key+","+directory_infiles);
         MoveFile(directory_key, directory_infiles);
       }
     })
@@ -271,6 +270,48 @@ function isTooLongFileName(val) {
   return false;
 }
 
+//압축 시작(백단에 파일들과 파일정보 map 전송)
+function RequestCompress() {
+  //디렉토리 리스트
+  var dirList = new Array();
+  for (var i = 0; i < directories.keys().length; i++) {
+    dirList.push(directories.get(i));
+  }
+
+  //파일이 몇번째 디렉토리에 들어있는지에 대한 정보 json값
+  var fileKeyPair = "{";
+  for (var i = 0; i < files.keys().length; i++) {
+    fileKeyPair +=
+      '"' +
+      files.get(i).get("file").name +
+      '" : "' +
+      files.get(i).get("directory") +
+      '"';
+      if (files.keys().length -1 != i){
+        fileKeyPair+=", ";
+      }
+  }
+  fileKeyPair += "}";
+
+  var formdata = new FormData();
+  for (var i = 0; i < files.keys().length; i++) {
+    formdata.append("files", files.get(i).get("file"));
+  }
+  formdata.append("fileKeyPair", fileKeyPair);
+  formdata.append("dirList", dirList);
+
+  $.ajax({
+    type: "post",
+    url: "/webzip/fileupload",
+    data: formdata,
+    processData: false,
+    contentType: false
+  }).done(function(data){
+    alert(data);
+  })
+
+}
+
 //초기세팅(bullet, sort, 기본 zip label 세팅)
 SetBulletIcon();
 SetSortable();