I am calling an ajax.action link on a page. This will display the name of the document. While i click on the document, an ajax request is fired tot he controller, which will return a File Content Result and I want this file to be shown inline in the browser under the targetID div.
Code - bytestream = fs.ToArray();
fs.Close();
Response.AppendHeader("Content-Disposition", String.Format("inline; filename={0}", fileName));
return File(bytestream, "application/pdf");
The problem is , the file is displayed as the stream and it is not displying the contents correctly.
<legend>Document</legend>
<% if (Model.PresentDocument != null)
{ %>
<li><%: Ajax.ActionLink(Model.PresentDocument, "GetDocumentPage", new RouteValueDictionary(new { controller = "Document", action = "GetDocumentPage", id = Model.PresDocId }), new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "Document" })%></li>
<%} %>
<div id="Document">
</div>
Do i need to do anything specific for this div to display the inline pdf?
You cannot use AJAX to download files. One possible way to achieve this is to have a normal link and when this link is clicked use javascript to dynamically generate an iframe and point the iframe source to the controller action:
<%= Html.ActionLink(
Model.PresentDocument,
"GetDocumentPage",
"Document",
new { id = Model.PresDocId },
new { id = "displayPdf" }
) %>
which is you could AJAXify like this (using jQuery):
$(function() {
$('#displayPdf').click(function() {
$('#Document').html(
$('<iframe/>', {
src: this.href,
width: '300px',
height: '150px'
})
);
return false;
});
});
which assumes that your controller action is accessible through GET requests:
public ActionResult GetDocumentPage(string id)
{
byte[] pdf = ...
Response.AppendHeader("Content-Disposition", String.Format("inline; filename={0}", fileName));
return File(pdf, "application/pdf");
}
Related
I am setting up Jquery-File-Upload for my website. The script is you can see here:
http://blueimp.github.io/jQuery-File-Upload/
This script automatically creates preview thumbnails of image files, however, it allows to select any files (doc, pdf etc). If user selects such a file, the script shows error "File type not allowed" but doesn't show any thumbnail. I want to set up a default thumbnail image for all non-image files.
I modified jquery.fileupload-image.js file:
Original:
setImage:function(data,options){
if(data.preview&&!options.disabled){
data.files[data.index][options.name||'preview']=data.preview;
}
return data;
}
My modification:
setImage:function(data,options){
if(data.preview&&!options.disabled){
data.files[data.index][options.name||'preview']=data.preview;
} else {
data.files[data.index][options.name||'preview']='<img src="/images/default-thumbnail.png">';
}
return data;
}
It works perfectly but the problem is that I will use this script in different sections of my website and thumbnail size always will be different.
So, I need to define default thumbnail in my html file. I tried:
var defaultthumbnail = '<img src="/images/default-thumbnail.png">';
or in options:
defaultthumbnail: '<img src="/images/default-thumbnail.png">'
but it doesn't work. The script doesn't return image and doesn't show any error.
Any ideas?
<script>
$(function () {
var formData = $('#fileupload').serializeArray();
var defaultthumbnail = '<img src="/images/default-thumbnail.png">';
'use strict';
$('#fileupload').fileupload({
url:'//mydomain.com'
});
$('#fileupload').fileupload('option', {
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
autoUpload:false,
maxNumberOfFiles:10,
disableImageResize: /Android(?!.*Chrome)|Opera/
.test(window.navigator.userAgent)
});
if ($.support.cors) {
$.ajax({
url: $('#fileupload').fileupload('option', 'url'),
type: 'HEAD'
}).fail(function () {
$('<div class="alert alert-danger"/>')
.text('Upload server currently unavailable - ' +
new Date())
.appendTo('#fileupload');
});
}
});
</script>
Looks that Jquery-File-Upload project is abandoned...
So if you want to show a default thumbnail for unsupported file types, do the following:
<script>
var defaultthumbnail = '<img src="/images/default-thumbnail.png" />';
$(function () {
var formData = $('#fileupload').serializeArray();
'use strict';
..............
..............
</script>
in jquery.fileupload-image.js file
change
setImage:function(data,options){
if(data.preview&&!options.disabled){
data.files[data.index][options.name||'preview']=data.preview;
}
return data;
}
to
setImage:function(data,options){
if(data.preview&&!options.disabled){
data.files[data.index][options.name||'preview']=data.preview;
} else {
data.files[data.index][options.name||'preview']=defaultthumbnail;
}
return data;
}
I am trying show a portlet (that previously I have created) from another one, but the pop-up is empty.
First, I create the renderURL:
<liferay-portlet:renderURL var="testPopupURL" portletName="<%=rule.getBannerPortletId() %>" windowState="<%=LiferayWindowState.POP_UP.toString() %>"></liferay-portlet:renderURL>
and I do the link:
<aui:a href="#" onClick="showPopup('${testPopupURL}')">View</aui:a>
and this is the function showPopup:
function showPopup(url){
console.log("En el showPopup ");
AUI().ready('aui-dialog', 'aui-io', 'event', 'event-custom', function(A) {
window.myDialog = new A.Dialog({
title: 'Banner',
width: 640,
centered: true
}).plug(A.Plugin.DialogIframe, {
uri: url.toString(),
iframeCssClass: 'dialog-iframe'
}).render();
});
}
I put in the liferay-portlet.xml (of the portlet I want open in the pop-up) this:
<add-default-resource>true</add-default-resource>
The portlet is instanciable and the bannerPortletId is the porletId.
Any idea?
Thanks
Finally I get display the portlet. I created the url with javascript:
var url;
function createRenderURL(portletId) {
AUI().ready('liferay-portlet-url', function(A) {
var renderURL = Liferay.PortletURL.createRenderURL();
renderURL.setName("Banner");
renderURL .setPortletMode("edit");
renderURL .setWindowState("pop_up");
renderURL.setPortletId(portletId);
url = renderURL.toString();
});
}
The code to show de pop-up is the same, but I pass the portletId like a parameter and call the function createRenderURL.
var url;
function createRenderURL(portletId) {
console.log("en el createRender");
AUI().ready('liferay-portlet-url', function(A) {
var renderURL = Liferay.PortletURL.createRenderURL();
renderURL.setName("Banner");
renderURL .setPortletMode("edit");
renderURL .setWindowState("pop_up");
renderURL.setPortletId(portletId);
console.log(renderURL);
url = renderURL.toString();
});
}
I hope this can be useful for someone.
In Facebook status update box, when I type # and start typing and choose a name, say Steven Gerrard, from the friends list suggested by fb, my friend's name is highlighted in the textarea like this
I checked with Firebug and there's only
a div.highlighter which contains sort of formated text (Steven Gerrard is within b tags)
a textarea inside a div.uiTypeahead. Nothing interesting i could find
and a hidden input, that contains the actual text that will be posted: #[100001915747xxx:Steven Gerrard] is awesome
What is the secret trick behind this? Normal rich text editors like ckeditor usually have an iframe to display the text and an actual textarea to keep the original content. But in this case, I do not see anything. Someone please shed some lights?
I would like to make something like this but have no clue where to begin. Also, if I would like to display a small thumb next to my friend's name, is it possible at all?
Here is how it works:
You superpose the textarea (in front) and a div (behind) that will have the same size, and the same font size.
The textarea must have a transparent background, so we can see its text, but also see the div behind it.
The div behind it will have a white text and white background, so the text it contains will be transparent.
You set a hook on the textarea's keyup, and you process the text it contains as HTML: replace the line breaks by <br/>, replace the double spaces by , and also replace all the words that you want to highlight by a version surrounded by <span style="background-color: #D8DFEA;"></span>.
Since you can see the highlight div behind the textarea, and that the text the highlight div contains is perfectly aligned with the text in the textarea, and that the <span> is visible, you will have the illusion that the text in the textarea is highlighted.
I've written a quick example based on jquery so you can try it yourself, without too much code to analyze.
Here is a sample code you can just copy-paste-save and try:
This sample code will highlight a defined set of word, here: "hello" and "world".
I'll let you adapt it the way you want.
<html>
<head>
<title></title>
<!-- Load jQuery -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<!-- The javascript xontaining the plugin and the code to init the plugin -->
<script type="text/javascript">
$(function() {
// let's init the plugin, that we called "highlight".
// We will highlight the words "hello" and "world",
// and set the input area to a widht and height of 500 and 250 respectively.
$("#container").highlight({
words: ["hello","world"],
width: 500,
height: 250
});
});
// the plugin that would do the trick
(function($){
$.fn.extend({
highlight: function() {
// the main class
var pluginClass = function() {};
// init the class
// Bootloader
pluginClass.prototype.__init = function (element) {
try {
this.element = element;
} catch (err) {
this.error(err);
}
};
// centralized error handler
pluginClass.prototype.error = function (e) {
// manage error and exceptions here
//console.info("error!",e);
};
// Centralized routing function
pluginClass.prototype.execute = function (fn, options) {
try {
options = $.extend({},options);
if (typeof(this[fn]) == "function") {
var output = this[fn].apply(this, [options]);
} else {
this.error("undefined_function");
}
} catch (err) {
this.error(err);
}
};
// **********************
// Plugin Class starts here
// **********************
// init the component
pluginClass.prototype.init = function (options) {
try {
// the element's reference ( $("#container") ) is stored into "this.element"
var scope = this;
this.options = options;
// just find the different elements we'll need
this.highlighterContainer = this.element.find('#highlighterContainer');
this.inputContainer = this.element.find('#inputContainer');
this.textarea = this.inputContainer.find('textarea');
this.highlighter = this.highlighterContainer.find('#highlighter');
// apply the css
this.element.css('position','relative');
// place both the highlight container and the textarea container
// on the same coordonate to superpose them.
this.highlighterContainer.css({
'position': 'absolute',
'left': '0',
'top': '0',
'border': '1px dashed #ff0000',
'width': this.options.width,
'height': this.options.height,
'cursor': 'text'
});
this.inputContainer.css({
'position': 'absolute',
'left': '0',
'top': '0',
'border': '1px solid #000000'
});
// now let's make sure the highlit div and the textarea will superpose,
// by applying the same font size and stuffs.
// the highlighter must have a white text so it will be invisible
this.highlighter.css({
'padding': '7px',
'color': '#eeeeee',
'background-color': '#ffffff',
'margin': '0px',
'font-size': '11px',
'font-family': '"lucida grande",tahoma,verdana,arial,sans-serif'
});
// the textarea must have a transparent background so we can see the highlight div behind it
this.textarea.css({
'background-color': 'transparent',
'padding': '5px',
'margin': '0px',
'font-size': '11px',
'width': this.options.width,
'height': this.options.height,
'font-family': '"lucida grande",tahoma,verdana,arial,sans-serif'
});
// apply the hooks
this.highlighterContainer.bind('click', function() {
scope.textarea.focus();
});
this.textarea.bind('keyup', function() {
// when we type in the textarea,
// we want the text to be processed and re-injected into the div behind it.
scope.applyText($(this).val());
});
} catch (err) {
this.error(err);
}
return true;
};
pluginClass.prototype.applyText = function (text) {
try {
var scope = this;
// parse the text:
// replace all the line braks by <br/>, and all the double spaces by the html version
text = this.replaceAll(text,'\n','<br/>');
text = this.replaceAll(text,' ',' ');
// replace the words by a highlighted version of the words
for (var i=0;i<this.options.words.length;i++) {
text = this.replaceAll(text,this.options.words[i],'<span style="background-color: #D8DFEA;">'+this.options.words[i]+'</span>');
}
// re-inject the processed text into the div
this.highlighter.html(text);
} catch (err) {
this.error(err);
}
return true;
};
// "replace all" function
pluginClass.prototype.replaceAll = function(txt, replace, with_this) {
return txt.replace(new RegExp(replace, 'g'),with_this);
}
// don't worry about this part, it's just the required code for the plugin to hadle the methods and stuffs. Not relevant here.
//**********************
// process
var fn;
var options;
if (arguments.length == 0) {
fn = "init";
options = {};
} else if (arguments.length == 1 && typeof(arguments[0]) == 'object') {
fn = "init";
options = $.extend({},arguments[0]);
} else {
fn = arguments[0];
options = $.extend({},arguments[1]);
}
$.each(this, function(idx, item) {
// if the component is not yet existing, create it.
if ($(item).data('highlightPlugin') == null) {
$(item).data('highlightPlugin', new pluginClass());
$(item).data('highlightPlugin').__init($(item));
}
$(item).data('highlightPlugin').execute(fn, options);
});
return this;
}
});
})(jQuery);
</script>
</head>
<body>
<div id="container">
<div id="highlighterContainer">
<div id="highlighter">
</div>
</div>
<div id="inputContainer">
<textarea cols="30" rows="10">
</textarea>
</div>
</div>
</body>
</html>
Let me know if you have any question or if you need help with this code.
After reviewing the way of Facebook do this, I see that the text shown on the screen is:
<span class="highlighterContent"><b>Ws Dev</b> is good</span>
That span is put in a table (with lots of div container), which is style accordingly.
So I think this is the process:
When you type in the box, Facebook does have a textarea that capture what you type, but use javascript to show the typed HTML content in a table.
When you submit, the formatted content in a hidden input (that you already spot in the question) get submitted. It's like "#[100001915747xxx:Steven Gerrard] is awesome".
When the formatted message submit, it is saved to the database. Everytime the page get loaded, from the saved message the HTML is composed and return.
To get the similar effect, you can use any jQuery autocomplete plugin.
I have to dynamically populate content into a DIV on clicking a hyperlink which should pass a value (code) to retrieve the content from server.
How can I achieve this using JQuery, Ajax and Hyperlink? I know how to retrieve content from server using JQuery & Ajax but I am struggling on how to Ajaxify an hyperlink and pass a value?
Well, you could start by giving this link an unique id so that you could reference it from client scripts:
<%= Html.ActionLink(
"link text",
"action",
"controller",
null,
new { id = "myLink" }
) %>
and then you could unobtrusively AJAXify it in a separate file javascript file:
$(function() {
$('#myLink').click(function() {
$.ajax({
url: this.href,
success: function(result) {
// TODO: handle the results here
}
});
return false;
});
});
Of course if the result of this action is a partial view HTML that you would like to inject on some placeholder in the DOM you could use the .load() method:
$(function() {
$('#myLink').click(function() {
$('#result').load(this.href);
return false;
});
});
YAnd if you want to pass some additional values with the AJAX request:
$.ajax({
url: this.href,
data: { someParam: 'some value', someOtherParam: 'some other value' },
success: function(result) {
// TODO: handle the results here
}
});
I've got a dropdownlist:
<%= Html.DropDownList("ddlNames", new SelectList(Model.NameList, "ID", "Name"))%>
I've got an ActionLink:
<%: Html.ActionLink("edit", "Edit", "Members", new { area = "MembersArea", id = XXX }, null)%>
I want the value of the dropdownlist in the XXX.
So I want to use values from controls on a view in the ActionLink.
Is that possible in a simple manner?
thanks,
Filip
You can't do this because the html helpers execute at the server side while the dropdown value can change at the client side. The only way to achieve it is to use javascript. You could register for the onchange event of the dropdown and modify the value of the href of the anchor:
$(function() {
$('#ddlNames').change(function() {
var value = this.value; // get the selected value
// TODO: modify the value of the anchor
});
});
This is probably not the best solution because the routes are configured on the server side and in order to modify the value of the link you need to do some string manipulation on the client side.
As an alternative you could use a form and a submit button instead of an anchor. This way the selected value of the dropdown will be automatically sent to the server and you don't need any javascript:
<% using (Html.BeginForm("Edit", "Members", new { area = "MembersArea" })) { %>
<%= Html.DropDownListFor(x => x.SelectedName,
new SelectList(Model.NameList, "ID", "Name"))%>
<input type="submit" value="Edit" />
<% } %>
Instead of modifying the value of the anchor every time a relevant dropdown is changed, just modify it once, on click.
Example using Razor:
#Html.DropDownList("DropDownFirstNames", new SelectList(Model.FirstNames, "ID", "Name"))
#Html.DropDownList("DropDownLastNames", new SelectList(Model.LastNames, "ID", "Name"))
#Html.ActionLink("Submit name", "ActionName", "ControllerName", null, new { #id = "SubmitName" })
<script type="text/javascript">
$('#SubmitName').click(function () {
var first = $('#DropDownFirstNames').val();
var last = $('#DropDownLastNames').val();
var path = '#Url.Content("~/ControllerName/ActionName")' + "?firstId=" + first + "+&lastId=" + last
$(this).attr("href", path);
});
</script>