forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUploadService.cs
More file actions
71 lines (60 loc) · 1.82 KB
/
FileUploadService.cs
File metadata and controls
71 lines (60 loc) · 1.82 KB
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
using System;
using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel.Dispatcher;
using ServiceStack.Common.Extensions;
using ServiceStack.Common.Utils;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface;
using ServiceStack.Validation;
namespace ServiceStack.WebHost.IntegrationTests.Services
{
[DataContract]
[RestService("/fileuploads/{RelativePath*}", HttpMethods.Get)]
[RestService("/fileuploads", HttpMethods.Post)]
public class FileUpload
{
[DataMember]
public string RelativePath { get; set; }
}
[DataContract]
public class FileUploadResponse
{
[DataMember]
public string FileName { get; set; }
[DataMember]
public long ContentLength { get; set; }
[DataMember]
public string ContentType { get; set; }
[DataMember]
public string Contents { get; set; }
}
public class FileUploadService
: RestServiceBase<FileUpload>
{
public override object OnGet(FileUpload request)
{
if (request.RelativePath.IsNullOrEmpty())
throw new ArgumentNullException("RelativePath");
var filePath = ("~/" + request.RelativePath).MapHostAbsolutePath();
if (!File.Exists(filePath))
throw new FilterInvalidBodyAccessException(request.RelativePath);
var result = new HttpResult(new FileInfo(filePath));
return result;
}
public override object OnPost(FileUpload request)
{
if (this.RequestContext.Files.Length == 0)
throw new ValidationError("UploadError", "No such file exists");
var file = this.RequestContext.Files[0];
return new FileUploadResponse
{
FileName = file.FileName,
ContentLength = file.ContentLength,
ContentType = file.ContentType,
Contents = new StreamReader(file.InputStream).ReadToEnd(),
};
}
}
}