public class ImageController : Controller { public ActionResult Index() { return View(); } [HttpPost] public JsonResult Index(HttpPostedFileBase file) { var service = new ImageService(); byte[] data = new byte[file.ContentLength]; file.InputStream.Read(data, 0, file.ContentLength); var image = service.CreateImage(file.FileName, file.ContentType, data); JsonResult res = Json(new {image.Id}); res.ContentType = "text/html"; // , jquery.form (for iframe implementation) return res; } [OutputCache(Duration = 0)] public ActionResult ById(string id) { var service = new ImageService(); var image = service.GetImage(id); return new FileStreamResult(new MemoryStream(image.Data), image.ContentType); } }
class ImageService { static Dictionary<string, ImageData> _images = new Dictionary<string, ImageData>(); public ImageData CreateImage(string fileName, string contentType, byte[] data) { var image = new ImageData(fileName, contentType, data); _images.Add(image.Id, image); return image; } public ImageData GetImage(string id) { return _images[id]; } internal class ImageData { private byte[] _data; public string FileName { get; set; } public string ContentType { get; set; } public string Id { get; set; } public byte[] Data { get { return _data; } } public ImageData(string fileName, string contentType, byte[] data) { FileName = fileName; ContentType = contentType; _data = data; Id = Guid.NewGuid().ToString(); } } }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript" src="@Url.Content("~/Scripts/tiny_mce/tiny_mce_popup.js")"></script> <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.form.js")" type="text/javascript"></script> <style type="text/css"> #image {height: 262px;overflow: auto;width: 440px;} #file { opacity:0;position:relative;z-index: 2;-moz-opacity:0 ; filter:alpha(opacity: 0);width:100%;height:100% } </style> </head> <body> @using (Html.BeginForm("Index", "Image", FormMethod.Post, new { id = "upload", enctype = "multipart/form-data" })) { <div> <div> <fieldset> <legend> </legend> <div id="image"><img id="preview" alt=" " src="" /></div> <input id="file" type="file" name="file" onchange="$('#upload').submit();"/> </fieldset> </div> </div> <div> <input type="button" id="insert" name="insert" value="" /> <input type="button" id="cancel" name="cancel" value="" > </div> } </body> </html>
$(document).ready(function () { var options = { dataType: "json", beforeSubmit: function (data) { $('#preview').attr('src', '/Content/images/spinning.gif'); }, success: function (data) { $('#preview').attr('src', '/Image/ById/' + data.Id); } }; // $('#upload').ajaxForm(options); // $('#preview').click(function () { $('#file').click(); }); // Auto popup file select dialog //$('#file').click(); });
(function() { // Load plugin specific language pack tinymce.PluginManager.requireLangPack('imagelight'); tinymce.create('tinymce.plugins.ImagelightPlugin', { init : function(ed, url) { tinyMCE.activeEditor.execCommand('mceExample'); ed.addCommand('mceImagelight', function() { ed.windowManager.open({ file : '/Image', width : 480, height : 385, inline : 1 }, { plugin_url : url // Plugin absolute URL }); }); ed.addButton('imagelight', { title : 'imagelight.desc', cmd : 'mceImagelight', image : url + '/img/imagelight.png' }); ed.onNodeChange.add(function(ed, cm, n) { cm.setActive('imagelight', n.nodeName == 'IMG'); }); }, getInfo : function() { return { longname : 'Imagelight plugin', author : 'AldeFalco', authorurl : 'http://tinymce.moxiecode.com', infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/example', version : "1.0" }; } }); tinymce.PluginManager.add('imagelight', tinymce.plugins.ImagelightPlugin); })();
tinyMCEPopup.requireLangPack(); var ImagelightDialog = { init: function () { var options = { dataType: "json", beforeSubmit: function (data) { $('#preview').attr('src', '/Content/images/spinning.gif'); }, success: function (data) { $('#preview').attr('src', '/Image/ById/' + data.Id); } }; $('#upload').ajaxForm(options); $('#preview').click(function () { $('#file').click(); }); }, insert : function() { // Insert the contents from the input into the document tinyMCEPopup.editor.execCommand('mceInsertContent', false, $('#image').html()); tinyMCEPopup.close(); } }; tinyMCEPopup.onInit.add(ImagelightDialog.init, ImagelightDialog);
tinyMCE.addI18n('en.imagelight',{ desc : 'Light Image Uploader' }); tinyMCE.addI18n('en.imagelight_dlg',{ title : 'Upload image', preview : 'Preview', alt : 'Click here to upload an image file', });
<title>{#imagelight_dlg.title}</title> <script type="text/javascript" src="@Url.Content("~/Scripts/tiny_mce/plugins/imagelight/js/dialog.js")"></script> ... <div class="panel_wrapper"> <div id="general_panel" class="panel current"> <fieldset> <legend>{#imagelight_dlg.preview}</legend> <div id="image"><img id="preview" alt="{#imagelight_dlg.alt}" src="" /></div> <input id="file" type="file" name="file" onchange="$('#upload').submit();"/> </fieldset> </div> </div> <div class="mceActionPanel"> <input type="button" id="insert" name="insert" value="{#insert}" onclick="ImagelightDialog.insert();" /> <input type="button" id="cancel" name="cancel" value="{#cancel}" onclick="tinyMCEPopup.close();" /> </div> }
Source: https://habr.com/ru/post/120168/
All Articles