Exemple #1
0
        public async Task <IActionResult> Upload(UploadFileView model, IFormFile fileToUpload, CancellationToken cancellationToken)
        {
            string FileType = null;
            string FileName = null;

            byte[] Data = null;

            if (fileToUpload == null || fileToUpload.Length <= 0)
            {
                ModelState.AddModelError("fileToUpload", "You must select a file to upload.");
            }
            else
            {
                var extenion = fileToUpload.FileName.Substring(fileToUpload.FileName.IndexOf(char.Parse(".")) + 1,
                                                               fileToUpload.FileName.Length - fileToUpload.FileName.IndexOf(char.Parse(".")) - 1);

                if (!String.IsNullOrWhiteSpace(extenion))
                {
                    extenion = extenion.ToLower();
                }

                if (extenion != "jpg" && extenion != "jpeg" && extenion != "png" && extenion != "gif" && extenion != "gif" && extenion != "pdf" && extenion != "doc" &&
                    extenion != "docx" && extenion != "ppt" && extenion != "pptx" && extenion != "pps" && extenion != "ppsx" && extenion != "odt" &&
                    extenion != "xls" && extenion != "xlsx" && extenion != "mp3" && extenion != "m4a" && extenion != "ogg" && extenion != "wav" &&
                    extenion != "mp4" && extenion != "m4v" && extenion != "mov" && extenion != "wmv" && extenion != "avi" && extenion != "mpg" && extenion != "txt")
                {
                    ModelState.AddModelError("fileToUpload", string.Format("Document type ({0}) is not importable.", extenion));
                }

                if (fileToUpload.Length > 10000000)
                {
                    ModelState.AddModelError("fileToUpload", "Document is too large, must be smaller then 10MB.");
                }

                FileType = fileToUpload.ContentType;
                FileName = fileToUpload.FileName;

                var uploadedFile = new byte[fileToUpload.OpenReadStream().Length];
                fileToUpload.OpenReadStream().Read(uploadedFile, 0, uploadedFile.Length);

                Data = uploadedFile;
            }

            if (ModelState.IsValid)
            {
                switch ((FileUploadTypes)model.Type)
                {
                case FileUploadTypes.CallFile:
                    CallAttachment callAttachment = new CallAttachment();
                    callAttachment.CallId             = int.Parse(model.ResourceId);
                    callAttachment.CallAttachmentType = (int)CallAttachmentTypes.File;
                    callAttachment.Name     = model.Name;
                    callAttachment.FileName = FileName;
                    //callAttachment.FileType = FileType;
                    callAttachment.Size      = Data.Length;
                    callAttachment.Data      = Data;
                    callAttachment.Timestamp = DateTime.UtcNow;

                    if (!await _authorizationService.CanUserEditCallAsync(UserId, callAttachment.CallId))
                    {
                        Unauthorized();
                    }

                    await _callsService.SaveCallAttachmentAsync(callAttachment, cancellationToken);

                    return(RedirectToAction("ViewCall", "Dispatch", new { Area = "User", callId = callAttachment.CallId }));
                }
            }

            return(View(model));
        }