-
Notifications
You must be signed in to change notification settings - Fork 102
/
FileUploaderController.cs
136 lines (111 loc) · 4.36 KB
/
FileUploaderController.cs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using DevExtreme.MVC.Demos.Models.FileManagement;
using DevExtreme.MVC.Demos.Models.FileUploader;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
namespace DevExtreme.MVC.Demos.Controllers {
public class FileUploaderController : Controller {
#region FileUploading
public ActionResult FileUploading() {
return View();
}
[HttpPost]
public ActionResult Upload() {
var file = Request.Files["myFile"];
try {
// Write code that saves the 'file' file.
// Don't rely on or trust the FileName property without validation
} catch {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
return new EmptyResult();
}
#endregion
#region FileSelection
public ActionResult FileSelection() {
return View();
}
[HttpPost]
public ActionResult FileSelection(string firstName, string lastName, HttpPostedFileBase photo) {
ViewBag.FirstName = firstName;
ViewBag.LastName = lastName;
ViewBag.Photo = "[No photo]";
if(photo != null) {
// Write code that saves the 'photo' file.
// Don't rely on or trust the FileName property without validation.
ViewBag.Photo = photo.FileName;
}
return View("SubmissionResult");
}
#endregion
#region Validation
const long MaxFileSize = 4000000;
string[] ImageExtensions = { ".JPG", ".JPEG", ".GIF", ".PNG" };
public ActionResult Validation() {
return View();
}
[HttpPost]
public ActionResult UploadImage(HttpPostedFileBase imageFile) {
try {
var extension = Path.GetExtension(imageFile.FileName).ToUpperInvariant();
var isValidExtenstion = ImageExtensions.Contains(extension);
if(!isValidExtenstion) {
throw new InvalidOperationException();
}
// Write code that saves the 'imageFile' file.
// Don't rely on or trust the FileName property without validation.
} catch {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
return new EmptyResult();
}
[HttpPost]
public ActionResult UploadSmallFile(HttpPostedFileBase smallFile) {
try {
var isValidSize = smallFile.InputStream.Length <= MaxFileSize;
if(!isValidSize) {
throw new InvalidOperationException();
}
// Write code that saves the 'smallFile' file.
// Don't rely on or trust the FileName property without validation.
} catch {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
return new EmptyResult();
}
#endregion
#region ChunkUploading
public ActionResult ChunkUploading() {
return View();
}
[HttpPost]
public ActionResult UploadChunk(HttpPostedFileBase file) {
var chunkMetadata = Request.Form["chunkMetadata"];
try {
if(!string.IsNullOrEmpty(chunkMetadata)) {
var metaDataObject = JsonConvert.DeserializeObject<ChunkMetadata>(chunkMetadata);
// Write code that appends the 'file' file chunk to the temporary file.
// You can use the $"{metaDataObject.FileGuid}.tmp" name for the temporary file.
// Don't rely on or trust the FileName property without validation.
if(metaDataObject.Index == metaDataObject.TotalCount - 1) {
// Write code that saves the 'file' file.
// Don't rely on or trust the FileName property without validation.
}
}
} catch {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
return new EmptyResult();
}
#endregion
#region CustomDropzone
public ActionResult CustomDropzone() {
return View();
}
#endregion
}
}