diff --git a/Site5/Automation/UploadFileHandler.ashx b/Site5/Automation/UploadFileHandler.ashx index 5ed5270e..6bbae681 100644 --- a/Site5/Automation/UploadFileHandler.ashx +++ b/Site5/Automation/UploadFileHandler.ashx @@ -1,7 +1,9 @@ <%@ WebHandler Language="C#" Class="Swarmops.Frontend.Automation.FileTransferHandler" %> +using System; using System.Collections.Generic; using System.Configuration; +using System.Drawing; using System.IO; using System.Linq; using System.Web; @@ -113,9 +115,44 @@ namespace Swarmops.Frontend.Automation { private void UploadWholeFile (HttpContext context, List statuses) { for (int i = 0; i < context.Request.Files.Count; i++) { var file = context.Request.Files[i]; - file.SaveAs(StorageRoot + Path.GetFileName(file.FileName)); + string fullName = Path.GetFileName(file.FileName); - string fullName = Path.GetFileName(file.FileName); + // Store in MemoryStream in order to try to load as an image + + /* + MemoryStream ms = new MemoryStream(); + byte[] fileData = new byte[file.ContentLength]; + + file.InputStream.Read(fileData, 0, file.ContentLength); + ms.Write(fileData, 0, file.ContentLength); + ms.Position = 0;*/ + + // Try to load as image. If fails, not an acceptable file + + try + { + Image image = Image.FromStream(file.InputStream); + } + catch (Exception) + { + FilesStatus errorStatus = new FilesStatus(fullName, file.ContentLength); + errorStatus.error = "ERR_NOT_IMAGE"; + errorStatus.url = string.Empty; + errorStatus.delete_url = string.Empty; + errorStatus.thumbnail_url = string.Empty; + + statuses.Add(errorStatus); + // -1 for length means the file was NOT saved, and that it could not be parsed as image. + return; + } + + // TODO: Store to file name dependent on GUID + + // TODO: Store new image in db + + file.InputStream.Position = 0; + file.SaveAs(StorageRoot + Path.GetFileName(file.FileName)); + statuses.Add(new FilesStatus(fullName, file.ContentLength)); } } @@ -193,9 +230,9 @@ namespace Swarmops.Frontend.Automation { type = "image/png"; size = fileLength; progress = "1.0"; - url = HandlerPath + "FileTransferHandler.ashx?f=" + fileName; + url = HandlerPath + "UploadFileHandler.ashx?f=" + fileName; thumbnail_url = HandlerPath + "Thumbnail.ashx?f=" + fileName; - delete_url = HandlerPath + "FileTransferHandler.ashx?f=" + fileName; + delete_url = HandlerPath + "UploadFileHandler.ashx?f=" + fileName; delete_type = "DELETE"; } } diff --git a/Site5/Images/Icons/iconshock-invoice-redcross-32px.png b/Site5/Images/Icons/iconshock-invoice-redcross-32px.png new file mode 100644 index 00000000..6e812dd7 Binary files /dev/null and b/Site5/Images/Icons/iconshock-invoice-redcross-32px.png differ diff --git a/Site5/Pages/v5/Financial/FileExpenseClaim.aspx b/Site5/Pages/v5/Financial/FileExpenseClaim.aspx index 6639519c..5e3122d1 100644 --- a/Site5/Pages/v5/Financial/FileExpenseClaim.aspx +++ b/Site5/Pages/v5/Financial/FileExpenseClaim.aspx @@ -13,10 +13,12 @@ // The below weird construct circumvents a mono-server optimization bug that kills the string - var imageSuccess1 = "\x3Cimg src='"; - var imageSuccess2 = "/Images/Icons/iconshock-invoice-greentick-32px.png"; - var imageSuccess3 = "' /\x3E"; // needs to be separate var rather than inline because of mono-server optimization bug - var imageSuccess = imageSuccess1 + imageSuccess2 + imageSuccess3; + var imageFileStart = "\x3Cimg src='"; + var imageFileSuccess = "/Images/Icons/iconshock-invoice-greentick-32px.png"; + var imageFileFail = "/Images/Icons/iconshock-invoice-redcross-32px.png"; + var imageFileEnd = "' /\x3E"; // needs to be separate var rather than inline because of mono-server optimization bug + var imageUploadSuccess = imageFileStart + imageFileSuccess + imageFileEnd; + var imageUploadFail = imageFileStart + imageFileFail + imageFileEnd; $(document).ready(function () { $('#DropBudgets').combotree({ @@ -42,8 +44,14 @@ url: '/Automation/UploadFileHandler.ashx', dataType: 'json', done: function (e, data) { - $.each(data.files, function(index, file) { - $('#DivUploadCount').append(imageSuccess); + $.each(data.result, function(index, file) { + if (file.error == null || file.error.length < 1) { + $('#DivUploadCount').append(imageUploadSuccess); + } else { + $('#DivUploadCount').append(imageUploadFail); // Assume ERR_NOT_IMAGE + alertify.error('File "' + file.name + '" could not be uploaded. Is it really an image?'); + } + }); $('#DivProgressUpload').progressbar({ value: 100 }); $('#DivProgressUpload').fadeOut('400', function () { $('#DivUploadCount').fadeIn(); }); @@ -57,6 +65,7 @@ }, add: function (e, data) { $('#DivUploadCount').css('display', 'none'); + $('#DivProgressUpload').progressbar({ value: 0 }); // reset in case of previous uploads on this page $('#DivProgressUpload').fadeIn(); data.submit(); } @@ -133,7 +142,7 @@ -
 
+