Manage File Attachments in ASP.NET MVC Rich Text Editor
The Rich Text Editor allows you to attach a file based on the file upload. You can attach your files using the file upload or drag-and-drop from your local path. When the file upload gets success, the attachment link inserts into the content.
In the below sample, configure the saveUrl and path properties to achieve file attachments.
-
saveUrl: Provides the service URL to save the files. -
path: Specifies the location where the uploaded file will be stored.
The following sample illustrates how to attach a file in the Rich Text Editor.
To configure the server-side handler, refer to the following code.
int x = 0;
string file;
[AcceptVerbs("Post")]
public void Save()
{
try
{
var httpPostedFile = System.Web.HttpContext.Current.Request.Files["UploadFiles"];
file = httpPostedFile.FileName;
if (httpPostedFile != null)
{
Console.WriteLine(System.Web.HttpContext.Current.Server.MapPath("~/Files"));
var fileSave = System.Web.HttpContext.Current.Server.MapPath("~/Files");
if (!Directory.Exists(fileSave))
{
Directory.CreateDirectory(fileSave);
}
var fileName = Path.GetFileName(httpPostedFile.FileName);
var fileSavePath = Path.Combine(fileSave, fileName);
while (System.IO.File.Exists(fileSavePath))
{
file = "rte" + x + "-" + fileName;
fileSavePath = Path.Combine(fileSave, file);
x++;
}
if (!System.IO.File.Exists(fileSavePath))
{
httpPostedFile.SaveAs(fileSavePath);
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.Clear();
Response.Headers.Add("name", file);
Response.ContentType = "application/json; charset=utf-8";
Response.StatusDescription = "File uploaded succesfully";
Response.Headers.Add("url", fileSavePath);
Response.End();
}
}
}
catch (Exception e)
{
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.StatusCode = 204;
Response.Status = "204 No Content";
Response.StatusDescription = e.Message;
Response.End();
}
}