@@ -69,7 +69,7 @@ public Task FileManagerRequest(ICompatibleHttpContext context)
69
69
String currentDirPath = "" ;
70
70
String moveUpDirPath = "" ;
71
71
72
- String dirPath = EnvUtil . GetBaseDirectory ( ) + this . _rootPath ;
72
+ String dirPath = EnvUtil . GetBaseDirectory ( ) + this . _rootPath ;
73
73
String dirName = context . Request . Query ( "dir" ) ;
74
74
if ( ! String . IsNullOrEmpty ( dirName ) )
75
75
{
@@ -199,7 +199,6 @@ public Task FileManagerRequest(ICompatibleHttpContext context)
199
199
}
200
200
201
201
202
-
203
202
/// <summary>
204
203
/// 处理上传
205
204
/// </summary>
@@ -225,10 +224,10 @@ public Task UploadRequest(ICompatibleHttpContext context)
225
224
ICompatiblePostedFile imgFile = context . Request . File ( "imgFile" ) ;
226
225
if ( imgFile == null )
227
226
{
228
- return this . showError ( context , "请选择文件。" ) ;
227
+ return this . showError ( context , "请选择文件。" ) ;
229
228
}
230
229
231
- String dirPath = EnvUtil . GetBaseDirectory ( ) + this . _rootPath ;
230
+ String dirPath = EnvUtil . GetBaseDirectory ( ) + this . _rootPath ;
232
231
if ( ! Directory . Exists ( dirPath ) )
233
232
{
234
233
Directory . CreateDirectory ( dirPath ) . Create ( ) ;
@@ -243,21 +242,21 @@ public Task UploadRequest(ICompatibleHttpContext context)
243
242
244
243
if ( ! extTable . ContainsKey ( dirName ) )
245
244
{
246
- return this . showError ( context , "目录名不正确。" ) ;
245
+ return this . showError ( context , "目录名不正确。" ) ;
247
246
}
248
247
249
248
String fileName = imgFile . GetFileName ( ) ;
250
249
String fileExt = Path . GetExtension ( fileName ) . ToLower ( ) ;
251
250
252
251
if ( imgFile . GetLength ( ) > maxSize )
253
252
{
254
- return this . showError ( context , "上传文件大小超过限制。" ) ;
253
+ return this . showError ( context , "上传文件大小超过限制。" ) ;
255
254
}
256
255
257
256
if ( String . IsNullOrEmpty ( fileExt ) ||
258
257
Array . IndexOf ( ( ( String ) extTable [ dirName ] ) . Split ( ',' ) , fileExt . Substring ( 1 ) . ToLower ( ) ) == - 1 )
259
258
{
260
- return this . showError ( context , "上传文件扩展名是不允许的扩展名。\n 只允许" + ( ( String ) extTable [ dirName ] ) + "格式。" ) ;
259
+ return this . showError ( context , "上传文件扩展名是不允许的扩展名。\n 只允许" + ( ( String ) extTable [ dirName ] ) + "格式。" ) ;
261
260
}
262
261
263
262
//创建文件夹
@@ -290,11 +289,7 @@ public Task UploadRequest(ICompatibleHttpContext context)
290
289
targetPath = dirPath + newFileName ;
291
290
}
292
291
293
- using ( FileStream fs = new FileStream ( targetPath , FileMode . Create ) )
294
- {
295
- imgFile . CopyToAsync ( fs ) ;
296
- fs . Flush ( ) ;
297
- }
292
+ SaveFile ( imgFile , targetPath ) ;
298
293
299
294
String fileUrl = saveUrl + newFileName ;
300
295
@@ -304,12 +299,21 @@ public Task UploadRequest(ICompatibleHttpContext context)
304
299
return context . Response . WriteAsync ( JsonAnalyzer . ToJson ( hash ) ) ;
305
300
}
306
301
307
- private Task showError ( ICompatibleHttpContext context , string message )
302
+ private async void SaveFile ( ICompatiblePostedFile imgFile , string targetPath )
303
+ {
304
+ using ( FileStream fs = new FileStream ( targetPath , FileMode . Create ) )
305
+ {
306
+ await imgFile . CopyToAsync ( fs ) ;
307
+ fs . Flush ( ) ;
308
+ }
309
+ }
310
+
311
+ private Task showError ( ICompatibleHttpContext context , string message )
308
312
{
309
313
Hashtable hash = new Hashtable { [ "error" ] = 1 , [ "message" ] = message } ;
310
314
return context . Response . WriteAsync ( JsonAnalyzer . ToJson ( hash ) ) ;
311
315
}
312
-
316
+
313
317
public class NameSorter : IComparer
314
318
{
315
319
public int Compare ( object x , object y )
0 commit comments