Bläddra i källkod

[WebApiUtils] Get uploaded file by name

develop
Robin Thoni 9 år sedan
förälder
incheckning
e9a8a974c3

+ 9
- 2
WebAPiUtils-test/Controllers/ValuesController.cs Visa fil

@@ -46,9 +46,16 @@ namespace WebAPiUtils_test.Controllers
46 46
 
47 47
         [HttpPost]
48 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 Visa fil

@@ -92,7 +92,7 @@ namespace iiie.WebApiUtils.BusinessManager
92 92
         /// </summary>
93 93
         /// <returns>The file path</returns>
94 94
         [NonAction]
95
-        public async Task<OpResult<string>> SaveFileToTemp()
95
+        public async Task<OpResult<DboUploadFile>> SaveFileToTemp(string name = null)
96 96
         {
97 97
             IEnumerable<HttpContent> parts = null;
98 98
             try
@@ -102,30 +102,36 @@ namespace iiie.WebApiUtils.BusinessManager
102 102
             }
103 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 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 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 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 124
                         memoryStream.WriteTo(streamWriter);
120 125
                         memoryStream.Close();
121 126
                         streamWriter.Close();
122 127
                     }
123 128
                 }
124
-                return OpResult<string>.Ok(path);
129
+
130
+                return OpResult<DboUploadFile>.Ok(dbo);
125 131
             }
126 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 Visa fil

@@ -0,0 +1,9 @@
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 Visa fil

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

Laddar…
Avbryt
Spara