Skip to content

Commit 67e0a37

Browse files
committed
Removed Useless items.
Added Signature validation and autoUpload
1 parent 05f9304 commit 67e0a37

File tree

4 files changed

+83
-77
lines changed

4 files changed

+83
-77
lines changed

jQuery-File-Upload.MVC3/Content/FileUpload/jquery.fileupload-ui.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
margin-bottom: 5px;
3535
}
3636
.files .progress {
37-
width: 200px;
37+
width: 100px;
3838
}
3939
.progress-animated .bar {
4040
background: url(../img/progressbar.gif) !important;

jQuery-File-Upload.MVC3/Controllers/HomeController.cs

+39-21
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,49 @@ public ActionResult UploadFiles()
8787
//}
8888

8989

90-
private void UploadPartialFile(string fileName, HttpRequestBase request, List<ViewDataUploadFilesResult> statuses)
90+
private void UploadPartialFile(string fileName, HttpRequestBase request, ICollection<ViewDataUploadFilesResult> statuses)
9191
{
92-
if (request.Files.Count != 1) throw new HttpRequestValidationException("Attempt to upload chunked file containing more than one fragment per request");
92+
if (request.Files.Count != 1)
93+
throw new HttpRequestValidationException(
94+
"Attempt to upload chunked file containing more than one fragment per request");
9395

9496
var file = request.Files[0];
9597

96-
if (fileName != null)
98+
if (file != null)
9799
{
98-
var fullName = Path.Combine(StorageRoot, Path.GetFileName(fileName));
100+
var fileStreamFromRequest = file.InputStream;
101+
var fullName = StorageRoot + Path.GetFileName(fileName);
99102

100-
using (var files = new FileStream(fullName, FileMode.Append, FileAccess.Write))
103+
if (!string.IsNullOrEmpty(request.Form["maxChunkSize"]))
101104
{
102-
if (file != null) file.InputStream.CopyTo(files);
103-
files.Close();
104-
}
105+
var maxChunkSize = Convert.ToInt64(request.Form["maxChunkSize"]);
106+
var useSignatureFile = request.Form["useSignatureFile"];
107+
var associatedSignatureFile = request.Form["certificateFileName[]"];
108+
var isSignatureFile = fileName.Equals(associatedSignatureFile) ? "yes" : "no";
109+
var last = fileStreamFromRequest.Length < maxChunkSize; //FUCK THIS LINE OF CODE!!!!!!!
105110

106-
statuses.Add(new ViewDataUploadFilesResult
107-
{
108-
name = fileName,
109-
size = file.ContentLength,
110-
type = file.ContentType,
111-
url = "/Home/Download/" + fileName,
112-
delete_url = "/Home/Delete/" + fileName,
113-
//thumbnail_url = @"data:image/png;base64," + EncodeFile(fullName),
114-
delete_type = "GET",
115-
});
111+
using (var fs = new FileStream(fullName, FileMode.Append, FileAccess.Write))
112+
{
113+
fileStreamFromRequest.CopyTo(fs);
114+
fs.Close();
115+
}
116+
117+
if (last)
118+
{
119+
120+
}
121+
122+
statuses.Add(new ViewDataUploadFilesResult
123+
{
124+
name = fileName,
125+
size = file.ContentLength,
126+
type = file.ContentType,
127+
url = string.Empty,
128+
delete_url = string.Empty,
129+
delete_type = "GET",
130+
isSignatureFile = isSignatureFile
131+
});
132+
}
116133
}
117134
}
118135

@@ -123,7 +140,7 @@ private void UploadWholeFile(HttpRequestBase request, List<ViewDataUploadFilesRe
123140
{
124141
var file = request.Files[i];
125142

126-
if (file != null)
143+
if (file != null && file.FileName != null)
127144
{
128145
var fullPath = Path.Combine(StorageRoot, Path.GetFileName(file.FileName));
129146

@@ -134,8 +151,8 @@ private void UploadWholeFile(HttpRequestBase request, List<ViewDataUploadFilesRe
134151
name = file.FileName,
135152
size = file.ContentLength,
136153
type = file.ContentType,
137-
url = "/Home/Download/" + file.FileName,
138-
delete_url = "/Home/Delete/" + file.FileName,
154+
url = string.Empty,
155+
delete_url = string.Empty,
139156
//thumbnail_url = @"data:image/png;base64," + EncodeFile(fullPath),
140157
delete_type = "GET",
141158
});
@@ -153,5 +170,6 @@ public class ViewDataUploadFilesResult
153170
public string delete_url { get; set; }
154171
public string thumbnail_url { get; set; }
155172
public string delete_type { get; set; }
173+
public string isSignatureFile { get; set; }
156174
}
157175
}

jQuery-File-Upload.MVC3/Upload/UploadHandler.ashx.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,15 @@ private void UploadFile(HttpContext context)
100100
}
101101

102102
// Upload partial file
103-
private void UploadPartialFile(string fileName, HttpContext context, List<FilesStatus> statuses)
103+
private void UploadPartialFile(string fileName, HttpContext context, ICollection<FilesStatus> statuses)
104104
{
105105
if (context.Request.Files.Count != 1)
106106
throw new HttpRequestValidationException(
107107
"Attempt to upload chunked file containing more than one fragment per request");
108108

109109
var fileStreamFromRequest = context.Request.Files[0].InputStream;
110+
//var file = context.Request.Files[0];
111+
//var contenttype = file.ContentType;
110112
var fullName = StorageRoot + Path.GetFileName(fileName);
111113

112114
if (!string.IsNullOrEmpty(context.Request.Form["maxChunkSize"]))

jQuery-File-Upload.MVC3/Views/Home/Index.cshtml

+40-54
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<!-- Shim to make HTML5 elements usable in older Internet Explorer versions -->
2323
<!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
2424

25-
<form id="fileupload" action="/Upload/UploadHandler.ashx" method="POST" enctype="multipart/form-data">
26-
@*<form id="fileupload" action="@Url.Action("UploadFiles")" method="POST" enctype="multipart/form-data">*@
25+
@*<form id="fileupload" action="/Upload/UploadHandler.ashx" method="POST" enctype="multipart/form-data">*@
26+
<form id="fileupload" action="@Url.Action("UploadFiles")" method="POST" enctype="multipart/form-data">
2727
<input type="hidden" id="maxChunkSize" name="maxChunkSize" value="0" />
2828

2929
<div class="row fileupload-buttonbar">
@@ -34,6 +34,10 @@
3434
<span>Add files...</span>
3535
<input type="file" name="files[]" multiple>
3636
</span>
37+
<span>
38+
Quero usar ficheiro de assinatura
39+
<input type="checkbox" id="useSignatureFile" onchange="ToggleSignatureFiles();" />
40+
</span>
3741
@*<button type="reset" class="btn btn-warning cancel">
3842
<i class="icon-ban-circle icon-white"></i>
3943
<span>Cancel upload</span>
@@ -52,15 +56,13 @@
5256
<th>File</th>
5357
<th>Description</th>
5458
@*<th>Size</th>*@
55-
<th>Signed?</th>
56-
<th>Embedded?</th>
57-
<th>Select Certificate</th>
59+
<th hidden>Signature File</th>
5860
<th>Progress</th>
5961
<th>Action</th>
6062
</tr>
6163
<tbody class="files"></tbody>
6264
</table>
63-
<div class="pull-right fileupload-buttonbar">
65+
<div class="pull-right fileupload-buttonbar">
6466
<button type="submit" class="btn btn-primary start">
6567
<i class="icon-upload icon-white"></i>
6668
<span>Upload</span>
@@ -73,26 +75,23 @@
7375
<script id="template-upload" type="text/x-tmpl">
7476
{% for (var i=0, file; file=o.files[i]; i++) { %}
7577
<tr class="template-upload fade">
76-
<td class="name"><span>{%=file.name%}</span></td>
77-
<td>
78+
<td class="name">
79+
<span>{%=file.name%}</span>
80+
</td>
81+
<td class="description">
7882
<input type="text" id="fileDescription" name="fileDescription[]" class="text-box" />
7983
</td>
8084
@*<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>*@
8185
{% if (file.error) { %}
8286
<td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
8387
{% } else if (o.files.valid && !i) { %}
88+
{% if($("#useSignatureFile").prop('checked')){ %}
8489
<td>
85-
<input type="checkbox" id="chkIsSigned" name="chkIsSigned[]" onclick="ToggleNextElement(this);" />
86-
</td>
87-
<td>
88-
<input type="checkbox" id="chkIsEmbedded" name="chkIsEmbedded[]" disabled onclick="ToggleNextElement(this);" />
89-
</td>
90-
<td>
91-
{% if(!globalIsNextFileSignature) { %}
92-
<input type="file" id="certificateFile" name="certificateFile[]" disabled onclick="SetSignatureVariable();" onchange="AddToUploadList(this);" />
90+
{% }else{ %}
91+
<td hidden>
92+
{% } %}
93+
<input type="file" id="certificateFile" name="certificateFile[]" onchange="AddToUploadList(this);" />
9394
<input type="hidden" name="certificateFileName[]" />
94-
{%= SetSignatureVariable() %}
95-
{% } %}
9695
</td>
9796
<td>
9897
<div class="progress progress-success progress-striped active">
@@ -116,7 +115,7 @@
116115
{% } %}
117116
</td>
118117
{% } else { %}
119-
<td colspan="2"></td>
118+
<td></td>
120119
{% } %}
121120
</tr>
122121
{% } %}
@@ -136,21 +135,21 @@
136135
<td class="error" colspan="2">
137136
<span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}
138137
</td>
138+
{% } else if(file.isSignatureFile == "yes") { %}
139+
@*mostra nada*@
139140
{% } else { %}
140141
<td class="name">
141142
<span>{%=file.name%}</span>
142143
@*<a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}">{%=file.name%}</a>*@
143144
</td>
144-
<td class="description"><span>{%=document.getElementById("fileDescription").value%}</span></td>
145+
<td class="description">
146+
<span>{%=document.getElementById("fileDescription").value%}</span>
147+
</td>
145148
@*<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>*@
146-
<td class="size"><span>{%=document.getElementById("chkIsSigned").checked ? 'Yes' : 'No'%}</span></td>
147-
<td class="size"><span>{%=document.getElementById("chkIsEmbedded").checked ? 'Yes' : 'No'%}</span></td>
148-
{% if(!globalIsNextFileSignature) { %}
149149
<td class="description"><span>{%=document.getElementById("certificateFile").value ? document.getElementById("certificateFile").value : 'N/A' %}</span></td>
150-
{% } %}
151150
<td><span class="label btn-success">Success!</span></td>
152-
{% } %}
153151
<td>N/A</td>
152+
{% } %}
154153
@*<td class="delete">
155154
<button class="btn btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
156155
<i class="icon-trash icon-white"></i>
@@ -163,8 +162,6 @@
163162

164163
<script type="text/javascript">
165164
166-
var globalIsNextFileSignature = false;
167-
168165
$(document).ready(function () {
169166
170167
InitializeFileUploader();
@@ -177,36 +174,31 @@
177174
$("#maxChunkSize").val($("#fileupload").fileupload('option').maxChunkSize);
178175
}
179176
180-
function ToggleNextElement(sender) {
181-
182-
var elementToToggle = $(sender).parent().next().children();
183-
var nextElement = $(elementToToggle).parent().next().children();
184-
185-
if (elementToToggle.attr('disabled')) {
186-
elementToToggle.removeAttr('disabled');
187-
} else {
188-
elementToToggle.attr('disabled', 'disabled');
189-
}
190-
191-
//Para o caso de desactivar o primeiro quando os outros estao activos
192-
if (elementToToggle.attr('disabled')) {
193-
elementToToggle.removeAttr('checked');
194-
nextElement.attr('disabled', 'disabled');
195-
}
196-
177+
function ToggleSignatureFiles(hide) {
178+
//$('td:nth-child(3),th:nth-child(3)').toggle(); // CSS 3, Nao funca no ie <= 8;
179+
$('td:first-child+*+*,th:first-child+*+*').toggle();
197180
}
198181
199182
function AddToUploadList(sender) {
200183
201-
$('#fileupload').fileupload('add', { files: $(sender).prop("files") });
184+
//set certificateFileName
185+
$(sender).next().attr('value', $(sender).prop("files")[0].name);
186+
187+
//disable file selector
188+
$(sender).attr('disabled', 'disabled');
189+
190+
//send signature file
191+
$('#fileupload').fileupload('send', { files: $(sender).prop("files") });
202192
203193
//var signatureFile = $(sender).prop("files");
204194
205195
//signatureFile.prop('isSignatureFile', 'true');
206196
207197
//$('#fileupload').fileupload('add', { files: signatureFile });
208198
209-
$(sender).next().attr('value', $(sender).prop("files")[0].name);
199+
200+
//$(sender).prop("files").setAttribute('oi', 'ola');
201+
//alert($(sender).prop("files").getAttribute('oi'));
210202
}
211203
212204
function InitializeFileUploader() {
@@ -217,16 +209,10 @@
217209
218210
$('#fileupload').fileupload('option', {
219211
maxFileSize: 9999999999,
220-
maxChunkSize: 1024 * 512, //512 KB
221-
singleFileUploads: 10,
222-
autoUpload: false
212+
maxChunkSize: 1024 * 1024, //512 KB
213+
autoUpload: false,
223214
});
224215
}
225216
226-
function SetSignatureVariable() {
227-
228-
globalIsNextFileSignature = true;
229-
}
230-
231217
</script>
232218

0 commit comments

Comments
 (0)