예제 #1
0
        public ActionResult UploadFileValidation()
        {
            string fileRoute = "/Public/";

            Func <string, string, bool> validationFunction = (filePath, mimeType) => {
                long size = new System.IO.FileInfo(filePath).Length;
                if (size > 10 * 1024 * 1024)
                {
                    return(false);
                }

                return(true);
            };

            FroalaEditor.FileOptions options = new FroalaEditor.FileOptions
            {
                Fieldname  = "myFile",
                Validation = new FroalaEditor.FileValidation(validationFunction)
            };

            try
            {
                return(Json(FroalaEditor.Image.Upload(System.Web.HttpContext.Current, fileRoute, options)));
            }
            catch (Exception e)
            {
                return(Json(e));
            }
        }
예제 #2
0
        public IActionResult UploadFilesManagerValidation(Microsoft.AspNetCore.Http.IFormFile file)
        {
            string fileRoute    = "wwwroot/";
            var    fileMimeType = file.ContentType;

            string[] ImageMimeTypes = new string[] { "image/gif", "image/jpeg", "audio/mpeg", "image/png", "image/webp" };
            if (!ImageMimeTypes.Contains(fileMimeType.ToString()))
            {
                return(Json(new Exception("Invalid contentType. It must be Image")));
            }

            Func <string, string, bool> validationFunction = (filePath, mimeType) => {
                long size = new System.IO.FileInfo(filePath).Length;
                if (size > 10 * 1024 * 1024)
                {
                    return(false);
                }
                MagickImageInfo info = new MagickImageInfo(filePath);

                if (info.Width != info.Height)
                {
                    return(false);
                }

                return(true);
            };

            FroalaEditor.FileOptions options = new FroalaEditor.FileOptions
            {
                Fieldname  = "file",
                Validation = new FroalaEditor.FileValidation(validationFunction)
            };

            try
            {
                return(Json(FroalaEditor.Image.Upload(HttpContext, fileRoute, options)));
            }
            catch (Exception e)
            {
                return(Json(e));
            }
        }
예제 #3
0
        public IHttpActionResult UploadFileAsync()
        {
            string uploadPath = _pathFile;

            FroalaEditor.FileOptions options = new FroalaEditor.FileOptions
            {
                Fieldname  = "file",
                Validation = new FroalaEditor.FileValidation(
                    new string[]
                {
                    "txt",
                    "pdf",
                    "doc",
                    "docx",
                    "xls",
                    "xlsx"
                },
                    new string[]
                {
                    "text/plain",
                    "application/msword",
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                    "application/x-pdf",
                    "application/pdf",
                    "application/x-excel",
                    "application/xls",
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    "application/vnd.ms-powerpoint",
                    "application/vnd.openxmlformats-officedocument.presentationml.presentation"
                })
            };
            try
            {
                return(Json(FroalaEditor.File.Upload(System.Web.HttpContext.Current, uploadPath, options)));
            }
            catch (Exception e)
            {
                return(Json(e));
            }
        }
        private IFormFile GetFileToUpload(FileOptions options = null)
        {
            // Use default file options.
            if (options == null)
            {
                options = defaultOptions;
            }

            if (!CheckContentType())
            {
                throw new Exception("Invalid contentType. It must be " + MultipartContentType);
            }

            var httpRequest = _httpContextAccessor.HttpContext.Request;

            int filesCount = 0;

            filesCount = httpRequest.Form.Files.Count;


            if (filesCount == 0)
            {
                throw new Exception("No file found");
            }

            // Get HTTP posted file based on the fieldname.

            var file = httpRequest.Form.Files.GetFile(options.Fieldname);


            if (file == null)
            {
                throw new Exception("Fieldname is not correct. It must be: " + options.Fieldname);
            }

            return(file);
        }
예제 #5
0
        public IActionResult UploadFileValidation()
        {
            var fileRoute = "wwwroot/";

            Func <string, string, bool> validationFunction = (filePath, mimeType) => {
                var size = new System.IO.FileInfo(filePath).Length;
                return(size <= 10 * 1024 * 1024);
            };

            var options = new FroalaEditor.FileOptions
            {
                Fieldname  = "myFile",
                Validation = new FroalaEditor.FileValidation(validationFunction)
            };

            try
            {
                return(Json(FroalaEditor.Image.Upload(HttpContext, fileRoute, options)));
            }
            catch (Exception e)
            {
                return(Json(e));
            }
        }