码迷,mamicode.com
首页 > 其他好文 > 详细

Rest文件上传

时间:2014-04-30 17:16:14      阅读:411      评论:0      收藏:0      [点我收藏+]

标签:com   http   blog   style   class   div   img   code   java   c   log   

文件上传时传过来一个stream

代码如下:

mamicode.com,码迷
 1  /// <summary>
 2         /// 上传文件
 3         /// </summary>
 4         /// <param name="fileName">文件名</param>
 5         /// <param name="fileStream">文件流</param>
 6         /// <returns></returns>
 7         public ResponseType<int> UploadFile(string projectId, string DocId, string LastModifier, Stream fileStream)
 8         {
 9             ResponseType<int> result = new ResponseType<int>();
10             string Path = string.Empty;
11             try
12             {
13                 if (fileStream == null)
14                 {
15                     throw new Exception("没有上传文件");
16                 }
17                 string relativePath = fileInfoBLL.GetFilePath(projectId, DocId);
18                 Path = System.IO.Path.Combine(filePath, relativePath);
19                 string fileName = fileInfoBLL.SaveFie(Path, fileStream);
20                          
21                 FilesInfo filesInfo = new FilesInfo();
22                 filesInfo.DocManageId = Convert.ToInt32(DocId);
23                 filesInfo.FileName = fileName;
24                 filesInfo.FilePath = System.IO.Path.Combine(relativePath, fileName);
25                 filesInfo.IsChecked = false;
26                 filesInfo.LastmodifiedDateTime = DateTime.Now;
27                 filesInfo.LastModifier = LastModifier;
28                 filesInfo.ProjectId = Convert.ToInt32(projectId);
29                 int fileId = fileInfoBLL.SaveFileInfo(filesInfo);
30                 if (fileId > 0)
31                 {
32                     result.IsSuccess = true;
33                     result.ErrorMessage = "文件保存成功!";
34                     result.Value = fileId;
35                 }
36             }
37 
38             catch (Exception ex)
39             {
40 
41                 ExceptionHandler.ExceptionHelper.Instance.HandleException(ex);
42                 result.IsSuccess = false;
43                 result.ErrorMessage = "文件保存失败!";
44             }
45 
46             return result;
47         }
mamicode.com,码迷

黄色部分为真正文件上传代码:

mamicode.com,码迷
 1    /// <summary>
 2         /// 保存文件
 3         /// </summary>
 4         /// <param name="relatePath">文件保存的路径</param>
 5         /// <param name="fileSteam">文件流</param>
 6         public string SaveFie(string relatePath, Stream fileSteam)
 7         {
 8             string fileName = string.Empty;
 9             try
10             {
11 
12                 if (!Directory.Exists(relatePath))  //判断文件目录
13                 {
14                     Directory.CreateDirectory(relatePath);
15                 }              
16 
17                 using (var ms = new MemoryStream())
18                 {
19                     fileSteam.CopyTo(ms);
20                     ms.Position = 0;
21 
22                     var encoding = Encoding.UTF8;
23                     var reader = new StreamReader(ms, encoding);
24                     var headerLength = 0L;
25 
26                     //读取第一行  
27                     var firstLine = reader.ReadLine();
28                     //计算偏移(字符串长度+回车换行2个字符)  
29                     headerLength += encoding.GetBytes(firstLine).LongLength + 2;
30 
31                     //读取第二行  
32                     var secondLine = reader.ReadLine();
33                     //计算偏移(字符串长度+回车换行2个字符)  
34                     headerLength += encoding.GetBytes(secondLine).LongLength + 2;
35                     //解析文件名  
36                  
37                     fileName = new System.Text.RegularExpressions.Regex("filename=\"(?<fn>.*)\"").Match(secondLine).Groups["fn"].Value;
38 
39                     //一直读到空行为止  
40                     while (true)
41                     {
42                         //读取一行  
43                         var line = reader.ReadLine();
44                         //若到头,则直接返回  
45                          if (line == null )
46                     
47                             break;
48                         //若未到头,则计算偏移(字符串长度+回车换行2个字符)  
49                          headerLength += encoding.GetBytes(line).LongLength + 2;
50                     
51                         if (string.IsNullOrEmpty(line))
52                             break;
53                     }
54 
55                     //设置偏移,以开始读取文件内容  
56                     ms.Position = headerLength;
57                     ////减去末尾的字符串:“\r\n--\r\n”  
58                     ms.SetLength(ms.Length - encoding.GetBytes(firstLine).LongLength - 3 * 2);
59 
60                     using (var fileToupload = new FileStream(System.IO.Path.Combine(relatePath,fileName), FileMode.Create))
61                     {
62                         ms.CopyTo(fileToupload);
63                         fileToupload.Close();
64                         fileToupload.Dispose();
65                     }
66                 }               
67             }
68             catch (Exception ex)
69             {
70                 ExceptionHandler.ExceptionHelper.Instance.HandleException(ex);
71             }
72 
73             return fileName;
74         }
mamicode.com,码迷

Rest文件上传,码迷,mamicode.com

Rest文件上传

标签:com   http   blog   style   class   div   img   code   java   c   log   

原文地址:http://www.cnblogs.com/lihongchen/p/3699143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!