/
HomeController.cs
151 lines (128 loc) · 5.13 KB
/
HomeController.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace jQuery_File_Upload.MVC3.Controllers
{
public class HomeController : Controller
{
private string StorageRoot
{
get { return Path.Combine(Server.MapPath("~/Files")); }
}
public ActionResult Index()
{
return View();
}
//DONT USE THIS IF YOU NEED TO ALLOW LARGE FILES UPLOADS
[HttpGet]
public void Delete(string id)
{
var filename = id;
var filePath = Path.Combine(Server.MapPath("~/Files"), filename);
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
}
//DONT USE THIS IF YOU NEED TO ALLOW LARGE FILES UPLOADS
[HttpGet]
public void Download(string id)
{
var filename = id;
var filePath = Path.Combine(Server.MapPath("~/Files"), filename);
var context = HttpContext;
if (System.IO.File.Exists(filePath))
{
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
context.Response.ContentType = "application/octet-stream";
context.Response.ClearContent();
context.Response.WriteFile(filePath);
}
else
context.Response.StatusCode = 404;
}
//DONT USE THIS IF YOU NEED TO ALLOW LARGE FILES UPLOADS
[HttpPost]
public ActionResult UploadFiles()
{
var r = new List<ViewDataUploadFilesResult>();
foreach (string file in Request.Files)
{
var statuses = new List<ViewDataUploadFilesResult>();
var headers = Request.Headers;
if (string.IsNullOrEmpty(headers["X-File-Name"]))
{
UploadWholeFile(Request, statuses);
}
else
{
UploadPartialFile(headers["X-File-Name"], Request, statuses);
}
JsonResult result = Json(statuses);
result.ContentType = "text/plain";
return result;
}
return Json(r);
}
private string EncodeFile(string fileName)
{
return Convert.ToBase64String(System.IO.File.ReadAllBytes(fileName));
}
//DONT USE THIS IF YOU NEED TO ALLOW LARGE FILES UPLOADS
//Credit to i-e-b and his ASP.Net uploader for the bulk of the upload helper methods - https://github.com/i-e-b/jQueryFileUpload.Net
private void UploadPartialFile(string fileName, HttpRequestBase request, List<ViewDataUploadFilesResult> statuses)
{
if (request.Files.Count != 1) throw new HttpRequestValidationException("Attempt to upload chunked file containing more than one fragment per request");
var file = request.Files[0];
var fullName = Path.Combine(StorageRoot, Path.GetFileName(fileName));
using (var fs = new FileStream(fullName, FileMode.Append, FileAccess.Write))
{
file.InputStream.CopyTo(fs);
}
statuses.Add(new ViewDataUploadFilesResult()
{
name = fileName,
size = file.ContentLength,
type = file.ContentType,
url = "/Home/Download/" + fileName,
delete_url = "/Home/Delete/" + fileName,
thumbnail_url = @"data:image/png;base64," + EncodeFile(fullName),
delete_type = "GET",
});
}
//DONT USE THIS IF YOU NEED TO ALLOW LARGE FILES UPLOADS
//Credit to i-e-b and his ASP.Net uploader for the bulk of the upload helper methods - https://github.com/i-e-b/jQueryFileUpload.Net
private void UploadWholeFile(HttpRequestBase request, List<ViewDataUploadFilesResult> statuses)
{
for (int i = 0; i < request.Files.Count; i++)
{
var file = request.Files[i];
var fullPath = Path.Combine(StorageRoot, Path.GetFileName(file.FileName));
file.SaveAs(fullPath);
statuses.Add(new ViewDataUploadFilesResult()
{
name = file.FileName,
size = file.ContentLength,
type = file.ContentType,
url = "/Home/Download/" + file.FileName,
delete_url = "/Home/Delete/" + file.FileName,
thumbnail_url = @"data:image/png;base64," + EncodeFile(fullPath),
delete_type = "GET",
});
}
}
}
public class ViewDataUploadFilesResult
{
public string name { get; set; }
public int size { get; set; }
public string type { get; set; }
public string url { get; set; }
public string delete_url { get; set; }
public string thumbnail_url { get; set; }
public string delete_type { get; set; }
}
}