public IActionResult UploadFileValidation() { 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 FileValidation(validationFunction) }; try { return(Json(Image.Upload(HttpContext, fileRoute, options))); } catch (Exception e) { return(Json(e)); } }
/// <summary> /// Uploads a file to disk. /// </summary> /// <param name="httpContext">The HttpContext object containing information about the request.</param> /// <param name="fileRoute">Server route where the file will be uploaded. This route must be public to be accesed by the editor.</param> /// <param name="options">File options.</param> /// <returns>Object with link.</returns> public static object Upload(HttpContext httpContext, string fileRoute, FileOptions options = null) { // Use default file options. if (options == null) { options = defaultOptions; } if (!CheckContentType(httpContext)) { throw new Exception("Invalid contentType. It must be " + MultipartContentType); } var httpRequest = httpContext.Request; int filesCount = 0; #if netcore filesCount = httpRequest.Form.Files.Count; #else filesCount = httpRequest.Files.Count; #endif if (filesCount == 0) { throw new Exception("No file found"); } // Get HTTP posted file based on the fieldname. #if netcore var file = httpRequest.Form.Files.GetFile(options.Fieldname); #else var file = httpRequest.Files.Get(options.Fieldname); #endif if (file == null) { throw new Exception("Fieldname is not correct. It must be: " + options.Fieldname); } // Generate Random name. string extension = Utils.GetFileExtension(file.FileName); string name = $"{Utils.GenerateUniqueString()}.{extension}"; string link = fileRoute + name; // Bug Fixes in File.cs #2 // https://github.com/froala/wysiwyg-editor-dotnet-sdk/issues/2 // Create directory if it doesn't exist. var fileRoutePath = new FileInfo(File.GetAbsoluteServerPath(fileRoute)); if (fileRoutePath.Directory != null && !fileRoutePath.Directory.Exists) { fileRoutePath.Directory.Create(); } // Copy contents to memory stream. Stream stream; #if netcore stream = new MemoryStream(); file.CopyTo(stream); stream.Position = 0; #else stream = file.InputStream; #endif String serverPath = File.GetAbsoluteServerPath(link); // Save file to disk. Save(stream, serverPath, options); // Check if the file is valid. if (options.Validation != null && !options.Validation.Check(serverPath, file.ContentType)) { // Bug Fixes in File.cs #2 // https://github.com/froala/wysiwyg-editor-dotnet-sdk/issues/2 // Delete "link" Delete(link); throw new Exception("File does not meet the validation."); } // Make sure it is compatible with ASP.NET Core. return(new { link = link.Replace("wwwroot/", "") }); }
internal static object Upload(HttpContext httpContext, string fileRoute, FroalaEditor.FileOptions options) { //Use default file options. //if (options == null) //{ // options = defaultOptions; //} if (!CheckContentType(httpContext)) { throw new Exception("Invalid contentType. It must be " + MultipartContentType); } var httpRequest = 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); } // Generate Random name. string extension = Utils.GetFileExtension(file.FileName); string name = Utils.GenerateUniqueString() + "." + extension; string link = fileRoute + name; // Create directory if it doesn't exist. FileInfo dir = new FileInfo(fileRoute); dir.Directory.Create(); // Copy contents to memory stream. Stream stream; stream = new MemoryStream(); file.CopyTo(stream); stream.Position = 0; String serverPath = GetAbsoluteServerPath(link); // Save file to disk. //Save(stream, serverPath, options); // Check if the file is valid. if (options.Validation != null && !options.Validation.Check(serverPath, file.ContentType)) { // Delete file. Delete(serverPath); throw new Exception("File does not meet the validation."); } // Make sure it is compatible with ASP.NET Core. return(new { link = link.Replace("wwwroot", "") }); }