Winform文件上傳

近期在作了一個winform的項目的附件上傳的需求web

最初項目選型的時候有三種數據庫

1.使用webservice、webapi上傳文件api

2,直接保存在數據庫中服務器

3.使用共享目錄+dos命令函數

 

第一種有文件大小限制、設計到的知識比較多,第二種會給數據庫增長不小的壓力,因而最後選了第三種ui

下面上關鍵代碼,代碼很簡單,每一個函數都寫了說明spa

 1         /// <summary>
 2         /// 鏈接遠程目錄
 3         /// </summary>
 4         /// <param name="path"></param>
 5         /// <param name="userName"></param>
 6         /// <param name="passWord"></param>
 7         /// <returns></returns>
 8         public static bool connectState(string path=@"\\192.168.0.136\軟件", string userName= "administrator", string passWord="******")
 9         {
10             bool Flag = false;
11             Process proc = new Process();
12             try
13             {
14                 proc.StartInfo.FileName = "cmd.exe";
15                 proc.StartInfo.UseShellExecute = false;
16                 proc.StartInfo.RedirectStandardInput = true;
17                 proc.StartInfo.RedirectStandardOutput = true;
18                 proc.StartInfo.RedirectStandardError = true;
19                 proc.StartInfo.CreateNoWindow = true;
20                 proc.Start();
21                 //string dosLine1 = "net use * /del /y";
22                 string dosLine2 = "net use " + path + " " + passWord + " /user:" + userName;
23                 //proc.StandardInput.WriteLine(dosLine1);
24                 proc.StandardInput.WriteLine(dosLine2);
25                 proc.StandardInput.WriteLine("exit");
26                 while (!proc.HasExited)
27                 {
28                     proc.WaitForExit(1000);
29                 }
30                 string errormsg = proc.StandardError.ReadToEnd();
31                 proc.StandardError.Close();
32                 if (string.IsNullOrEmpty(errormsg))
33                 {
34                     Flag = true;
35                 }
36                 else
37                 {
38                     //throw new Exception(errormsg);
39                 }
40             }
41             catch (Exception ex)
42             {
43                 //throw ex;
44                 proc.Close();
45                 proc.Dispose();
46             }
47             finally
48             {
49                 proc.Close();
50                 proc.Dispose();
51             }
52             return Flag;
53         }
54         /// <summary>
55         /// 向遠程文件夾保存本地內容,或者從遠程文件夾下載文件到本地
56         /// </summary>
57         /// <param name="src">要保存的文件的路徑,若是保存文件到共享文件夾,這個路徑就是本地文件路徑如:@"D:\1.avi"</param>
58         /// <param name="dst">保存文件的路徑,不含名稱及擴展名</param>
59         /// <param name="fileName">保存文件的名稱以及擴展名</param>
60         public static void Transport(string src, string dst, string fileName)
61         {
62 
63             FileStream inFileStream = new FileStream(src, FileMode.Open);
64             if (!Directory.Exists(dst))
65             {
66                 Directory.CreateDirectory(dst);
67             }
68             dst = dst + fileName;
69             FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
70 
71             byte[] buf = new byte[inFileStream.Length];
72 
73             int byteCount;
74 
75             while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
76             {
77                 outFileStream.Write(buf, 0, byteCount);
78             }
79 
80             inFileStream.Flush();
81             inFileStream.Close();
82             outFileStream.Flush();
83             outFileStream.Close();
84 
85         }
86         /// <summary>
87         /// 避免保存到服務器上的文件有重複,使用Guid對文件進行重命名
88         /// </summary>
89         public static string GetGuid
90         {
91             get
92             {
93                 return Guid.NewGuid().ToString().ToLower();
94             }
95         }
相關文章
相關標籤/搜索