一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - ASP.NET教程 - ASP.NET 上傳文件到共享文件夾的示例

ASP.NET 上傳文件到共享文件夾的示例

2021-12-10 15:54大穩(wěn)·楊 ASP.NET教程

這篇文章主要介紹了ASP.NET 上傳文件到共享文件夾的示例,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下

創(chuàng)建共享文件夾參考資料

上傳文件代碼

  web.config 

?
1
2
3
4
5
<!--上傳文件配置,UploadPath值一定是服務(wù)器ip,內(nèi)網(wǎng)ip最好-->
<add key="UploadPath" value="\\172.21.0.10\File" />
<add key="DownloadPath" value="http://x.x.x.x:80/" />
<add key="UserName" value="ShareUser" />
<add key="Password" value="P@ssw0rd" />

  工具方法  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
public static string GetConfigString(string key, string @default = "")
        {
            return ConfigurationManager.AppSettings[key] ?? @default;
        }
 
    /// <summary>
    /// 根據(jù)文件名(包含文件擴(kuò)展名)獲取要保存的文件夾名稱
    /// </summary>
    public class FileHelper
    {
        /// <summary>
        /// 根據(jù)文件名(包含文件擴(kuò)展名)獲取要保存的文件夾名稱
        /// </summary>
        /// <param name="fileName">文件名(包含文件擴(kuò)展名)</param>
        public static string GetSaveFolder(string fileName)
        {
            var fs = fileName.Split('.');
            var ext = fs[fs.Length - 1];
            var str = string.Empty;
            var t = ext.ToLower();
            switch (t)
            {
                case "jpg":
                case "jpeg":
                case "png":
                case "gif":
                    str = "images";
                    break;
                case "mp4":
                case "mkv":
                case "rmvb":
                    str = "video";
                    break;
                case "apk":
                case "wgt":
                    str = "app";
                    break;
                case "ppt":
                case "pptx":
                case "doc":
                case "docx":
                case "xls":
                case "xlsx":
                case "pdf":
                    str = "file";
                    break;
                default:
                    str = "file";
                    break;
            }
 
            return str;
        }
    }
 
    /// <summary>
    /// 記錄日志幫助類
    /// </summary>
    public class WriteHelper
    {
        public static void WriteFile(object data)
        {
            try
            {
                string path = $@"C:\Log\";
                var filename = $"Log.txt";
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                TextWriter tw = new StreamWriter(Path.Combine(path, filename), true); //true在文件末尾添加數(shù)據(jù)
 
                tw.WriteLine($"----產(chǎn)生時(shí)間:{DateTime.Now:yyyy-MM-dd HH:mm:ss}---------------------------------------------------------------------");
 
                tw.WriteLine(data.ToJson());
                tw.Close();
            }
            catch (Exception e)
            {
 
            }
        }
    }

  常量

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/// <summary>
    /// 文件上傳配置項(xiàng)
    /// </summary>
    public class FileUploadConst
    {
        /// <summary>
        /// 上傳地址
        /// </summary>
        public static string UploadPath => ConfigHelper.GetConfigString("UploadPath");
 
        /// <summary>
        /// 文件訪問(wèn)/下載地址
        /// </summary>
        public static string DownloadPath => ConfigHelper.GetConfigString("DownloadPath");
 
        /// <summary>
        /// 訪問(wèn)共享目錄用戶名
        /// </summary>
        public static string UserName => ConfigHelper.GetConfigString("UserName");
 
        /// <summary>
        /// 訪問(wèn)共享目錄密碼
        /// </summary>
        public static string Password => ConfigHelper.GetConfigString("Password");
    }

  具體上傳文件代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/// <summary>
        /// 上傳文件到共享文件夾
        /// </summary>
        [HttpPost, Route("api/Upload/UploadAttachment")]
        [AllowAnonymous]
        public ServiceResponse<UploadRespModel> UploadAttachment()
        {
            var viewModel = new UploadRespModel();
            var code = 200;
            var msg = "上傳失敗!";
 
            var path = FileUploadConst.UploadPath; //@"\\172.16.10.130\Resource";
            var s = connectState(path, FileUploadConst.UserName, FileUploadConst.Password);
 
            if (s)
            {
                var filelist = HttpContext.Current.Request.Files;
                if (filelist.Count > 0)
                {
                    var file = filelist[0];
                    var fileName = file.FileName;
                    var blobName = FileHelper.GetSaveFolder(fileName);
                    path = $@"{path}\{blobName}\";
 
                    fileName = $"{DateTime.Now:yyyyMMddHHmmss}{fileName}";
 
                    //共享文件夾的目錄
                    var theFolder = new DirectoryInfo(path);
                    var remotePath = theFolder.ToString();
                    Transport(file.InputStream, remotePath, fileName);
 
                    viewModel.SaveUrl = $"{blobName}/{fileName}";
                    viewModel.DownloadUrl = PictureHelper.GetFileFullPath(viewModel.SaveUrl);
 
                    msg = "上傳成功";
                }
            }
            else
            {
                code = CommonConst.Code_OprateError;
                msg = "鏈接服務(wù)器失敗";
            }
 
            return ServiceResponse<UploadRespModel>.SuccessResponse(msg, viewModel, code);
        }
 
        /// <summary>
        /// 連接遠(yuǎn)程共享文件夾
        /// </summary>
        /// <param name="path">遠(yuǎn)程共享文件夾的路徑</param>
        /// <param name="userName">用戶名</param>
        /// <param name="passWord">密碼</param>
        private static bool connectState(string path, string userName, string passWord)
        {
            bool Flag = false;
            Process proc = new Process();
            try
            {
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
                WriteHelper.WriteFile($"dosLine:{dosLine}");
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (!proc.HasExited)
                {
                    proc.WaitForExit(1000);
                }
 
                string errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
                WriteHelper.WriteFile($"errormsg:{errormsg}");
                if (string.IsNullOrEmpty(errormsg))
                {
                    Flag = true;
                }
                else
                {
                    throw new Exception(errormsg);
                }
            }
            catch (Exception ex)
            {
                WriteHelper.WriteFile(ex);
                throw ex;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
 
            return Flag;
        }
 
        /// <summary>
        /// 向遠(yuǎn)程文件夾保存本地內(nèi)容,或者從遠(yuǎn)程文件夾下載文件到本地
        /// </summary>
        /// <param name="inFileStream">要保存的文件的路徑,如果保存文件到共享文件夾,這個(gè)路徑就是本地文件路徑如:@"D:\1.avi"</param>
        /// <param name="dst">保存文件的路徑,不含名稱及擴(kuò)展名</param>
        /// <param name="fileName">保存文件的名稱以及擴(kuò)展名</param>
        private static void Transport(Stream inFileStream, string dst, string fileName)
        {
            WriteHelper.WriteFile($"目錄-Transport:{dst}");
            if (!Directory.Exists(dst))
            {
                Directory.CreateDirectory(dst);
            }
 
            dst = dst + fileName;
 
            if (!File.Exists(dst))
            {
                WriteHelper.WriteFile($"文件不存在,開(kāi)始保存");
                var outFileStream = new FileStream(dst, FileMode.Create, FileAccess.Write);
 
                var buf = new byte[inFileStream.Length];
 
                int byteCount;
 
                while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
                {
                    outFileStream.Write(buf, 0, byteCount);
                }
                WriteHelper.WriteFile($"保存完成");
                inFileStream.Flush();
 
                inFileStream.Close();
 
                outFileStream.Flush();
 
                outFileStream.Close();
            }
        }

以上就是ASP.NET 上傳文件到共享文件夾的示例的詳細(xì)內(nèi)容,更多關(guān)于ASP.NET 上傳文件的資料請(qǐng)關(guān)注服務(wù)器之家其它相關(guān)文章!

原文鏈接:https://www.cnblogs.com/dawenyang/p/13493179.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: yjzz视频 | 桥本有菜在线四虎福利网 | 亚洲国产精品久久久久久网站 | 欧美成人在线影院 | 国产日韩欧美综合一区二区三区 | 亚洲H成年动漫在线观看不卡 | 性欧美金发洋妞xxxxbbbb | 视频二区 素人 制服 国产 | 日本人交换乱理伦片 | 欧美一区二区三区视视频 | 奇米影视在线视频 | 国产资源免费观看 | 欧美1 | 暖暖日本在线观看免费 | 国产精品亚洲精品日韩已满 | 日韩免费视频播播 | 精品亚洲一区二区三区在线播放 | 日韩一级片在线观看 | 5x社区在线观看直接进入 | 精品亚洲视频在线 | 久久r视频 | 免费观看一级特黄三大片视频 | 好姑娘在线视频观看免费 | 99热这里只有精品久久免费 | 深夜福利一区 | 国产伦精品一区二区 | 2021国产精品视频 | h玉足嫩脚嗯啊白丝 | 456在线观看| 国产成人免费片在线视频观看 | 精品国产免费久久久久久 | 国产欧美日韩综合 | 麻豆视频入口 | 91porny新九色在线 | 精品在线免费观看视频 | 亚洲精品午夜在线观看 | 男人操男人 | 小浪妇奶真大水多 | freese×video性欧美丝袜 | 婷婷去我也去 | 国产在线观看人成激情视频 |