Skip to content

Instantly share code, notes, and snippets.

@vanillajonathan
Last active October 31, 2023 12:42
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save vanillajonathan/53eeccd2b7fce2e1c73b to your computer and use it in GitHub Desktop.
Save vanillajonathan/53eeccd2b7fce2e1c73b to your computer and use it in GitHub Desktop.
TinyMCE image upload on ASP.NET MVC
<script src="//tinymce.cachefly.net/4.2/tinymce.min.js"></script>
<script>
tinymce.init({
selector: 'textarea',
images_upload_url: "TinyMceUpload",
});
function upload(form) {
tinymce.activeEditor.uploadImages(function (success) {
form.submit();
});
return false;
}
</script>
<br/>
@using (Html.BeginForm("Index", "TinyMce", FormMethod.Post, new { onsubmit = "return upload(this);" }))
{
<div class="form-group">
<textarea name="data" rows="10">Press Ctrl+V to paste an image!</textarea>
</div>
<input class="btn btn-success" type="submit" value="Save" />
}
using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication.Controllers
{
public class TinyMceController : Controller
{
// GET: TinyMce
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Index(string data)
{
var message = "Hello " + data;
return Content(message);
}
[HttpPost]
public ActionResult TinyMceUpload(HttpPostedFileBase file)
{
//Response.AppendHeader("Access-Control-Allow-Origin", "*");
var location = SaveFile(Server.MapPath("~/uploads/"), file);
return Json(new { location }, JsonRequestBehavior.AllowGet);
}
/// <summary>
/// Saves the contents of an uploaded image file.
/// </summary>
/// <param name="targetFolder">Location where to save the image file.</param>
/// <param name="file">The uploaded image file.</param>
/// <exception cref="InvalidOperationException">Invalid MIME content type.</exception>
/// <exception cref="InvalidOperationException">Invalid file extension.</exception>
/// <exception cref="InvalidOperationException">File size limit exceeded.</exception>
/// <returns>The relative path where the file is stored.</returns>
private static string SaveFile(string targetFolder, HttpPostedFileBase file)
{
const int megabyte = 1024 * 1024;
if (!file.ContentType.StartsWith("image/"))
{
throw new InvalidOperationException("Invalid MIME content type.");
}
var extension = Path.GetExtension(file.FileName.ToLowerInvariant());
string[] extensions = { ".gif", ".jpg", ".png", ".svg", ".webp" };
if (!extensions.Contains(extension))
{
throw new InvalidOperationException("Invalid file extension.");
}
if (file.ContentLength > (8 * megabyte))
{
throw new InvalidOperationException("File size limit exceeded.");
}
var fileName = Guid.NewGuid() + extension;
var path = Path.Combine(targetFolder, fileName);
file.SaveAs(path);
return Path.Combine("/uploads", fileName).Replace('\\', '/');
}
}
}
@ozanerturk
Copy link

So clear. Thanks.

@lofcz
Copy link

lofcz commented May 23, 2018

Page reloads on upload but image is stored successfully, any help?

@stevem26
Copy link

stevem26 commented Sep 5, 2018

Thank you this is really helpful.

@anilcrk
Copy link

anilcrk commented Aug 25, 2020

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment