Nancy之文件上傳與下載

零、前言

因爲前段時間一直在找工做,找到工做後又比較忙,又加班又通宵的趕項目,因此博客有段時間沒有更新了。html

今天稍微空閒一點,碰巧前幾天看到有園友問我Nancy中下載文件的問題,而後就趁着休息的時間寫下了這篇博客。git

直接進正題吧!
 

1、新建一個空的asp.net應用程序

經過nuget安裝相應的packages
 

2、添加Modules和Views文件夾

用於存放咱們的「控制器」和視圖(這一步不是必須的喔!)
 

3、新建CustomRootPathProvider.cs

具體以下:
    
1     public class CustomRootPathProvider : IRootPathProvider
2     {
3         public string GetRootPath()
4         {
5             return AppDomain.CurrentDomain.GetData(".appPath").ToString(); 
6         }
7     }

   

4、編寫Bootstrapper.cs

具體以下:
 
 1     public class Bootstrapper : DefaultNancyBootstrapper
 2     {
 3         protected override IRootPathProvider RootPathProvider
 4         {
 5             get
 6             {
 7                 return new CustomRootPathProvider();
 8             }
 9         }
10     }  

 

5、編寫Module

這裏我是新建了一個HomeModule.cs
須要注意的是,要在構造函數中添加一點東西
IRootPathProvider pathProvider  
 
下面貼上HomeModule.cs的完整代碼
 
 1 using Nancy;
 2 using System.Collections.Generic;
 3 using System.IO;
 4  
 5 namespace NancyUpLoadAndDownloadDemo.Modules
 6 {
 7     public class HomeModule : NancyModule
 8     {
 9         public HomeModule(IRootPathProvider pathProvider) : base("/")
10         {
11             var uploadDirectory = Path.Combine(pathProvider.GetRootPath(), "Content", "uploads");
12  
13             Get["/"] = _ =>
14             {
15                 return View["UpLoad"];
16             };
17  
18             Post["/"] = _ =>
19             {
20                 
21                 if (!Directory.Exists(uploadDirectory))
22                 {
23                     Directory.CreateDirectory(uploadDirectory);
24                 }
25  
26                 foreach (var file in Request.Files)
27                 {
28                     var filename = Path.Combine(uploadDirectory, file.Name);
29                     using (FileStream fileStream = new FileStream(filename, FileMode.Create))
30                     {
31                         file.Value.CopyTo(fileStream);
32                     }
33                 }                
34                 return Response.AsRedirect("/show") ;
35             };
36  
37             Get["/down/{name}"] = _ =>
38             {
39                 string fileName = _.name;
40                 var relatePath = @"Content\uploads\"+fileName;
41                 return Response.AsFile(relatePath);         
42             };
43  
44             Get["/show"] = _ =>
45             {                
46                 var folder = new DirectoryInfo(uploadDirectory);
47                 IList<string> files = new List<string>();
48                 foreach (var file in folder.GetFiles())
49                 {
50                     files.Add(file.Name);
51                 }
52                 return View["Show", files];
53             };
54         }
55     }    
56 }  

 

 
 
下面簡單說一下這些是用來幹嗎的:
Get["/"]   顯示upload這個頁面
Post["/"]   上傳文件的
Get["/down/{name}"]   下載文件,{name}是參數 文件名
Get["/show"]  顯示可下載的文件
 
上傳文件和下載文件的具體細節會在看完演示後細說。
 

6、創建視圖

 
Show.cshtml
 1 @{
 2     Layout = null;
 3 }
 4  
 5 <!DOCTYPE html>
 6  
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title></title>
11 </head>
12 <body>
13     <ul>
14         @foreach (var item in Model)
15         {
16             <li>
17                 <a href="/down/@item">
18                     @item
19                 </a>
20             </li>
21         }
22     </ul>
23 </body>
24 </html>
25  

 

UpLoad.cshtml
 
 1 @{
 2     Layout = null;
 3 }
 4  
 5 <!DOCTYPE html>
 6  
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title>UpLoad</title>
11 </head>
12 <body>
13     <h1>這是上傳文件的演示</h1>
14     <hr />
15     <form action="/" method="post" enctype="multipart/form-data">
16  
17         <div>
18             <label>請選擇要上傳的文件</label>
19             <input type="file" name="myFile" />
20         </div>
21         <div>
22             <input type="submit" value="上傳" />
23         </div>
24  
25     </form>
26 </body>
27 </html>

 

 
視圖就比較簡單,沒什麼樣式。就是簡單的列出文件名稱和上傳文件的表單
 
下面來看看效果:
1.png
2.png
3.png
 
 
 
 
就這樣完成了簡單的上傳和下載功能,也是挺簡單的。
 

7、上傳與下載的細節

上傳:
相信以前你們在asp.net中進行上傳時,下面這個httppostedfilebase類,確定是常常用的
 
可是這個是基於system.web的,而Nancy是不依賴於system.web的!!!那麼Nancy是怎麼處理這個的呢
 
Nancy有本身的一套東西來處理這個,能夠看看這個類
 
也能夠看看httppostfilebase這個類
 
像處理這些問題,Nancy都有本身的實現,用起來跟日常的用法有點區別,這個是須要注意的!!
 
還有一個要注意的是路徑的問題,這個問題能夠參見
 
下載:
在asp.net mvc中,下載咱們用的比較多的是 fileresult
 
其實,Nancy也提供了相似的方法
用法就是 response.asfile()
能夠參見下面的
相關文章
相關標籤/搜索