Browse Source

[WebApiUtils] Get uploaded file by name

develop
Robin Thoni 9 years ago
parent
commit
e9a8a974c3

+ 9
- 2
WebAPiUtils-test/Controllers/ValuesController.cs View File

46
 
46
 
47
         [HttpPost]
47
         [HttpPost]
48
         [Route("api/values/upload")]
48
         [Route("api/values/upload")]
49
-        public DboGetSingle<string> Upload()
49
+        public DboUploadFile Upload()
50
         {
50
         {
51
-            return HandleSingle(SaveFileToTemp().Result);
51
+            return Handle(SaveFileToTemp().Result);
52
+        }
53
+
54
+        [HttpPost]
55
+        [Route("api/values/upload2")]
56
+        public DboUploadFile Upload2()
57
+        {
58
+            return Handle(SaveFileToTemp("file").Result);
52
         }
59
         }
53
     }
60
     }
54
 }
61
 }

+ 14
- 8
WebApiUtils/BusinessManager/BMRHandler.cs View File

92
         /// </summary>
92
         /// </summary>
93
         /// <returns>The file path</returns>
93
         /// <returns>The file path</returns>
94
         [NonAction]
94
         [NonAction]
95
-        public async Task<OpResult<string>> SaveFileToTemp()
95
+        public async Task<OpResult<DboUploadFile>> SaveFileToTemp(string name = null)
96
         {
96
         {
97
             IEnumerable<HttpContent> parts = null;
97
             IEnumerable<HttpContent> parts = null;
98
             try
98
             try
102
             }
102
             }
103
             catch (Exception e)
103
             catch (Exception e)
104
             {
104
             {
105
-                return OpResult<string>.Error(ResultStatus.InputError, e, "Failed to read post parts (maximum size exceeded?)");
105
+                return OpResult<DboUploadFile>.Error(ResultStatus.InputError, e, "Failed to read post parts (maximum size exceeded?)");
106
             }
106
             }
107
-            HttpContent file = parts.FirstOrDefault();
107
+            HttpContent file = parts.FirstOrDefault(x => name == null || (x.Headers.ContentDisposition != null && x.Headers.ContentDisposition.Name.Trim('"') == name));
108
             if (file == null)
108
             if (file == null)
109
             {
109
             {
110
-                return OpResult<string>.Error(ResultStatus.InputError, "No uploaded file", "");
110
+                return OpResult<DboUploadFile>.Error(ResultStatus.InputError, "No uploaded file", "");
111
             }
111
             }
112
             try
112
             try
113
             {
113
             {
114
-                var path = Path.GetTempFileName();
114
+                DboUploadFile dbo = new DboUploadFile();
115
+                if (file.Headers.ContentDisposition != null)
116
+                {
117
+                    dbo.OriginalFilename = Uri.UnescapeDataString(file.Headers.ContentDisposition.FileName.Trim('"'));
118
+                }
119
+                dbo.TempFilePath = Path.GetTempFileName();
115
                 using (var memoryStream = new MemoryStream(await file.ReadAsByteArrayAsync()))
120
                 using (var memoryStream = new MemoryStream(await file.ReadAsByteArrayAsync()))
116
                 {
121
                 {
117
-                    using (var streamWriter = new FileStream(path, FileMode.OpenOrCreate))
122
+                    using (var streamWriter = new FileStream(dbo.TempFilePath, FileMode.OpenOrCreate))
118
                     {
123
                     {
119
                         memoryStream.WriteTo(streamWriter);
124
                         memoryStream.WriteTo(streamWriter);
120
                         memoryStream.Close();
125
                         memoryStream.Close();
121
                         streamWriter.Close();
126
                         streamWriter.Close();
122
                     }
127
                     }
123
                 }
128
                 }
124
-                return OpResult<string>.Ok(path);
129
+
130
+                return OpResult<DboUploadFile>.Ok(dbo);
125
             }
131
             }
126
             catch (Exception e)
132
             catch (Exception e)
127
             {
133
             {
128
-                return OpResult<string>.Error(ResultStatus.InputError, e, "Failed to write uploaded file");
134
+                return OpResult<DboUploadFile>.Error(ResultStatus.InputError, e, "Failed to write uploaded file");
129
             }
135
             }
130
         }
136
         }
131
     }
137
     }

+ 9
- 0
WebApiUtils/DBO/DboUploadFile.cs View File

1
+namespace iiie.WebApiUtils.DBO
2
+{
3
+    public class DboUploadFile
4
+    {
5
+        public string TempFilePath { get; set; }
6
+
7
+        public string OriginalFilename { get; set; }
8
+    }
9
+}

+ 1
- 0
WebApiUtils/WebApiUtils.csproj View File

98
     <Compile Include="BusinessManager\WebApiUtils.cs" />
98
     <Compile Include="BusinessManager\WebApiUtils.cs" />
99
     <Compile Include="DBO\DboGetMultiple.cs" />
99
     <Compile Include="DBO\DboGetMultiple.cs" />
100
     <Compile Include="DBO\DboGetSingle.cs" />
100
     <Compile Include="DBO\DboGetSingle.cs" />
101
+    <Compile Include="DBO\DboUploadFile.cs" />
101
     <Compile Include="Properties\AssemblyInfo.cs" />
102
     <Compile Include="Properties\AssemblyInfo.cs" />
102
   </ItemGroup>
103
   </ItemGroup>
103
   <ItemGroup>
104
   <ItemGroup>

Loading…
Cancel
Save