示例#1
0
        /// <summary>
        /// method returns a list of Selection
        /// </summary>
        async Task HandleSelection(IFileListEntry[] files)
        {
            // locals
            UploadedFileInfo uploadedFileInfo = null;
            bool             abort            = false;

            // locals
            MemoryStream ms = null;

            var file = files.FirstOrDefault();

            if (file != null)
            {
                try
                {
                    // the partialGuid is need to ensure uniqueness
                    string partialGuid = Guid.NewGuid().ToString().Substring(0, PartialGuidLength);

                    // create the uploadedFileInfo
                    uploadedFileInfo = new UploadedFileInfo(file, partialGuid, AppendPartialGuid, UploadFolder);

                    // if the file is too large
                    if ((MaxFileSize > 0) && (file.Size > MaxFileSize))
                    {
                        // Show the FileTooLargeMessage
                        status = FileTooLargeMessage;

                        // Upload was aborted
                        uploadedFileInfo.Aborted      = true;
                        uploadedFileInfo.ErrorMessage = FileTooLargeMessage;
                        uploadedFileInfo.Exception    = new Exception("The file uploaded was too large.");
                    }
                    else
                    {
                        // Create a new instance of a 'FileInfo' object.
                        FileInfo fileInfo = new FileInfo(file.Name);

                        // Set the extension. The ToLower is for just in case. I don't know if it's even possible for an extension to be upper case
                        uploadedFileInfo.Extension = fileInfo.Extension.ToLower();

                        // if FilterByExtension is true and the AllowedExtensions text exists
                        if ((FilterByExtension) && (!string.IsNullOrWhiteSpace(AllowedExtensions)))
                        {
                            // If the allowed extensions
                            abort = !AllowedExtensions.ToLower().Contains(fileInfo.Extension);
                        }

                        // Set aborted to true
                        uploadedFileInfo.Aborted = abort;

                        // if we should continue
                        if (!abort)
                        {
                            // create the memoryStream
                            ms = new MemoryStream();

                            // await for the data to be copied to the memory stream
                            await file.Data.CopyToAsync(ms);

                            // Check for abort 1 more time
                            uploadedFileInfo = CheckSize(fileInfo.Extension, ms, uploadedFileInfo);

                            // if abort
                            if (uploadedFileInfo.Aborted)
                            {
                                // Do not process due to size is not valid, message has already been set
                                abort = true;
                            }
                        }

                        // if we should continue
                        if (!abort)
                        {
                            // if the value for SaveToDisk is true
                            if (SaveToDisk)
                            {
                                // save the file using the FullName (If AppendPartialGuid is still true, than the Name.PartialGuid is the FullName
                                using (FileStream fileStream = new FileStream(Path.Combine(UploadFolder, uploadedFileInfo.FullName), FileMode.Create, FileAccess.Write))
                                {
                                    ms.WriteTo(fileStream);
                                }
                            }
                            else
                            {
                                // Set the MemoryStream, to allow people to save outside of the project
                                // folder, to disk or other processing like virus scan.
                                uploadedFileInfo.Stream = ms;
                            }

                            // if there is a CustomSave
                            if (!String.IsNullOrWhiteSpace(CustomSuccessMessage))
                            {
                                // Show the CustomSuccessMessage
                                status = CustomSuccessMessage;
                            }
                            else
                            {
                                // set the status
                                status = $"Saved file {file.Size} bytes from {file.Name}";
                            }

                            // Set additional properties for UploadFileInfo from this component; these values may be null.
                            uploadedFileInfo.CustomId = CustomId;
                            uploadedFileInfo.Tag      = Tag;

                            // The upload has completed
                            UploadComplete = true;
                        }
                        else
                        {
                            // If a CustomExtensionMessage has been set
                            if (!string.IsNullOrWhiteSpace(CustomExtensionMessage))
                            {
                                // Display the Custom extension doesn't validate message
                                uploadedFileInfo.ErrorMessage = CustomExtensionMessage;
                            }
                            else
                            {
                                // Can't think of a better message than this yet, just woke up
                                uploadedFileInfo.ErrorMessage = "The file uploaded is an invalid extension.";
                            }

                            // Show the exception
                            uploadedFileInfo.Exception = new Exception(uploadedFileInfo.ErrorMessage);
                        }
                    }
                }
                catch (Exception error)
                {
                    // Upload was aborted
                    uploadedFileInfo.Aborted = true;

                    // Store the Exception
                    uploadedFileInfo.Exception = error;

                    // if a CustomErrorMessage is set
                    if (!String.IsNullOrWhiteSpace(CustomErrorMessage))
                    {
                        // Show the custom error message
                        status = CustomErrorMessage;
                    }
                    else
                    {
                        // show the full error
                        status = error.ToString();
                    }

                    // set the error message
                    uploadedFileInfo.ErrorMessage = status;
                }
                finally
                {
                    // Notify the caller the upload was successful or aborted due to an error
                    FileUploaded(uploadedFileInfo);
                }
            }
        }
示例#2
0
        /// <summary>
        /// This method returns the Size
        /// </summary>
        public UploadedFileInfo CheckSize(string extension, MemoryStream ms, UploadedFileInfo uploadedFileInfo)
        {
            try
            {
                // if the file is a .jpg or a .png
                if (IsImageFile(extension))
                {
                    // get the image from the memory stream
                    Image image = Bitmap.FromStream(ms);

                    // set the properties for the return value
                    uploadedFileInfo.Height = image.Height;
                    uploadedFileInfo.Width  = image.Width;

                    // The RequiredHeight is an exact match

                    // if the RequiredHeight or RequiredWidth and a message is set the upload file
                    if (HasRequiredHeight || (HasRequiredWidth) && (HasCustomRequiredSizeMessage))
                    {
                        // check Required Size

                        // if a RequiredHeight is set
                        if ((HasRequiredHeight) && (image.Height != RequiredHeight))
                        {
                            // Abort for file being incorrect height
                            uploadedFileInfo.Aborted = true;
                        }
                        else if ((HasRequiredWidth) && (image.Width != RequiredWidth))
                        {
                            // Abort for file being incorrect width
                            uploadedFileInfo.Aborted = true;
                        }
                    }

                    // if not aborted
                    if (!uploadedFileInfo.Aborted)
                    {
                        // check Min Size

                        // if a MinHeight is set
                        if ((HasMinHeight) && (image.Height != MinHeight))
                        {
                            // Abort for file being incorrect height
                            uploadedFileInfo.Aborted = true;
                        }
                        else if ((HasMinWidth) && (image.Width != MinWidth))
                        {
                            // Abort for file being incorrect width
                            uploadedFileInfo.Aborted = true;
                        }

                        // if Aborted
                        if (uploadedFileInfo.Aborted)
                        {
                            // Set the Status
                            Status = CustomMinHeightMessage;
                        }
                    }

                    // if not aborted
                    if (!uploadedFileInfo.Aborted)
                    {
                        // check Max Size

                        // if a MaxHeight is set
                        if ((HasMaxHeight) && (image.Height != MaxHeight))
                        {
                            // Abort for file being incorrect height
                            uploadedFileInfo.Aborted = true;
                        }
                        else if ((HasMaxWidth) && (image.Width != MaxWidth))
                        {
                            // Abort for file being incorrect width
                            uploadedFileInfo.Aborted = true;
                        }

                        // if Aborted
                        if (uploadedFileInfo.Aborted)
                        {
                            // Set the Status
                            Status = CustomMaxHeightMessage;
                        }
                    }
                }
            }
            catch (Exception error)
            {
                // for debugging only
                string err = error.ToString();
            }

            // return value
            return(uploadedFileInfo);
        }
示例#3
0
 /// <summary>
 /// This event is fired after a file is uploaded, and is used to notify subscribers of the OnChange event.
 /// </summary>
 private void FileUploaded(UploadedFileInfo uploadedFileInfo)
 {
     // Notify the client a file was uploaded
     OnChange.InvokeAsync(uploadedFileInfo);
 }