Last active
February 7, 2017 18:33
-
-
Save Renancp01/c12e4612413b61737f5652bd6ad9f843 to your computer and use it in GitHub Desktop.
Gist Criado para integração do TinyMCE ASP.NET MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model EuVotoAf.Models.ViewModels.PublicaoViewModel | |
@{ | |
ViewBag.Title = "Create"; | |
Layout = "~/Views/Shared/_Layout.cshtml"; | |
} | |
<script src="~/Scripts/tinymce/tinymce.js"></script> | |
<h2>Create</h2> | |
@using (Html.BeginForm("Create", "Publicacao", FormMethod.Post)) | |
{ | |
@Html.AntiForgeryToken() | |
<div class="row"> | |
<div class="col-md-6"> | |
<label class="control-label col-md-4">Titulo</label> | |
@Html.TextBoxFor(model => model.Titulo, new { @class = "form-control col-md-4", @placeholder = "Digite o Titulo" }) | |
</div> | |
</div> | |
<div> | |
@Html.TextAreaFor(model => model.Conteudo, new { id = "my_editor", @class = "mceEditor" }) | |
</div> | |
<div class="form-group"> | |
<div class="col-md-offset-2 col-md-10"> | |
<input type="submit" value="Create" class="btn btn-default" /> | |
</div> | |
</div> | |
} | |
<input name="file" type="file" id="file"> | |
@section Scripts{ | |
@Scripts.Render("~/bundles/jqueryval") | |
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script> | |
<script type="text/javascript"> | |
tinymce.init({ | |
selector: "textarea", | |
theme: "modern", | |
width: 700, | |
plugins: [ | |
"advlist autolink lists link charmap print preview hr anchor pagebreak", | |
"searchreplace wordcount visualblocks visualchars code fullscreen", | |
"insertdatetime media nonbreaking save table contextmenu directionality", | |
"emoticons template paste textcolor colorpicker textpattern imagetools" | |
], | |
toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image", | |
toolbar2: "print preview media | forecolor backcolor emoticons | ltr rtl", | |
image_advtab: true, | |
templates: [ | |
{ title: 'Test template 1', content: 'Test 1' }, | |
{ title: 'Test template 2', content: 'Test 2' } | |
], | |
file_browser_callback: function (field_name, url, type, win) { | |
if (type == 'image') $('#my_form input').click(); | |
} | |
}); | |
</script> | |
<script> | |
$('#file').change(function () { | |
var formData = new FormData(); | |
var file = this.files[0]; | |
console.log(file) | |
formData.append('file', file); | |
$.ajax({ | |
url: '@Url.Action("Upload", "Publicacao")', | |
type: 'POST', | |
data: formData, | |
processData: false, | |
contentType: false, | |
success: function (data) { | |
console.log(data) | |
tinyMCE.get('my_editor').setContent('Digite seu texto'); | |
tinymce.activeEditor.execCommand('mceInsertContent', false, data); | |
} | |
}); | |
}); | |
</script> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public string Upload(HttpPostedFileBase file) | |
{ | |
string path; | |
string saveloc = "~/Uploads/Imagens"; | |
string filename = file.FileName; | |
if (file != null && file.ContentLength > 0) | |
{ | |
try | |
{ | |
path = Path.Combine(HttpContext.Server.MapPath(saveloc), Path.GetFileName(filename)); | |
file.SaveAs(path); | |
} | |
catch (Exception e) | |
{ | |
return "<script>alert('Failed: " + e + "');</script>"; | |
} | |
} | |
else | |
{ | |
return "<script>alert('Failed: Unkown Error. This form only accepts valid images.');</script>"; | |
} | |
return @"<img src=/Uploads/Imagens/" + filename + " >"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment