Display string in xml format in browser either using view or controller - asp.net-mvc-4

I have a string in my model.The string is actually XML content. I have a link on my page, when clicked it opens a new tab and displays the text as XML.
The result should be the same as when I right click on an xml file and open with Internet Explorer. The difference is that this is no file, its text that I need to display as XML in a new tab.
Anyone have an idea how to achieve this without creating a file and without giving a path to a file.

You could have a controller that will serve this XML and set the proper content type header:
public class MyXMLController: Controller
{
public ActionResult Index()
{
MyModel model = GetModelFromSomewhere(...);
return Content(model.StringPropertyContainingXML, "text/xml");
}
}
now all that's left is to write an anchor link pointing to /myxml/index:
#Html.ActionLink("Click to open XML", "index", "myxml", null, new { _target = "blank" })

Related

GWT Image Upload empty content

I am trying to implement GWT image upload functionality. I have made the required code change but for some reason upload is not happening. At the server side the image is not being received. So I checked at the client side (browser) the request header and content and then I found that Content-Length: 44 (just 44). Then I realized that the image is not being sent to server on from submission. Please check the below GWT code.
VerticalPanel vp = new VerticalPanel();
vp.add(CommonFormLayoutUtil.createLabel("Upload"));
final FormPanel form = new FormPanel();
form.setAction("CGIImageUpload");
// set form to use the POST method, and multipart MIME encoding.
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
final FileUpload fileUpload = new FileUpload();
Button uploadButton = new Button("Upload");
uploadButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
//get the filename to be uploaded
String filename = fileUpload.getFilename();
if (filename.length() == 0) {
showError("No File Specified!", null);
} else {
//submit the form
form.submit();
}
}
});
vp.add(fileUpload);
vp.add(uploadButton);
form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
#Override
public void onSubmitComplete(SubmitCompleteEvent event) {
// When the form submission is successfully completed, this
//event is fired. Assuming the service returned a response
//of type text/html, we can get the result text here
showError(event.getResults(), null);
}
});
form.add(vp);
Am i missing anything here? Please suggest.
Thanks.
FormPanel states the following:
"This panel can be used to achieve interoperability with servers that accept traditional HTML form encoding. The following widgets (those that implement com.google.gwt.user.client.ui.HasName) will be submitted to the server if they are contained within this panel" (emphasis mine)
You need to set the name of the FileUpload widget otherwise it will not be submitted by the FormPanel.
fileUpload.setName("someName");
Try setting this and it should work

Provide static file download at server in web application

I am working on an MVC 4 web application. On one page I am providing an anchor link which refers to a file on application's directory. The code of the same is -
#Html.Action("Download_Static_File", "Charge_Entry", new { File_Path = "../../Content/Templates/Pt_Data/Pt_Data.xls", File_Name = "Pt_Data_Template", value = "Download template" });
My motive is that the file should be downloaded on click.
However when I click the link, I get an error like
Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\Content\Templates\Pt_Data\Pt_Data.xls'.'
I also tried
System.Web.HttpContext.Current.Server.MapPath
which is giving this error:
OutputStream is not available when a custom TextWriter is used.
The action method being called is:
public FileResult Download_Static_File(string File_Path,string File_Name)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(File_Path);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, File_Name);
}
Is it the correct approach? Any help will be appreciated.
I also referred this link
Your anchor seem to be pointing to a controller action named Download_Static_File (which unfortunately you haven't shown) and passing a parameter called File_Path.
The #Html.Action helper that you are using in your view is attempting to execute the specified action as a child action. You may find the following blog post useful which describes child actions: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx/
I guess that what you are trying to achieve is to generate an anchor in your view pointing to the static file which can be downloaded by the user. In this case you'd rather use an anchor tag in conjunction with the Url.Action helper:
<a href="#Url.Content("~/Content/Templates/Pt_Data/Pt_Data.xls")">
Download_Static_File
</a>
This assumes that your web application has a folder called Content/Templates/Pt_Data under the root containing a file named Pt_Data.xls which will be downloaded by the user when he clicks upon this anchor tag.
If on the other hand the file that you want to be downloaded by the user is situated in a folder which is not publicly accessible from the client (for example the ~/App_Data folder) you might in this case have a controller action on your server that will stream the file:
public ActionResult DownloadStaticFile(string filename)
{
string path = Server.MapPath("~/App_Data");
string file = Path.Combine(path, filename);
file = Path.GetFullPath(file);
if (!file.StartsWith(path))
{
throw new HttpException(403, "Forbidden");
}
return File(file, "application/pdf");
}
and then in your view you would have an anchor to this controller action:
#Html.ActionLink(
linkText: "Download template",
actionName: "DownloadStaticFile",
controllerName: "Charge_Entry",
routeValues: new { filename = "Pt_Data.xls" },
htmlAttributes: null
)

Why isn't the page data tab showing in the Designer?

I'm editing a page in the ToolTwist Designer, and I have all the normal tabs shown - edit page, navpoint, test page, source, etc, but the "Page Data" tab is not showing. How can I make the tab show, so I can enter page data for my page?
The page data tab only appears if there is a widget on your page that requires page data. You also need to editing the navpoint, rather than the page, because the page data belongs to the navpoint, and a single page definition might be shared by many navpoints. In other words, the page data allows widgets to appear different at various locations (navpoints) within the website.
If you are developing a widget that you wish to have use page data, you need to do the following:
In the Widget Controller class, implement the "UsesPageData" interface. This tells the Designer that the page data tab needs to be displayed when you click on a navpoint that references a page that included this widget, and on the tab it creates a section where the XML for this widget can be entered, specific to that particular navpoint.
public class CarouselTab extends WbdWidgetController implements UsesPageData
To give the user an indication of what XML the widget expects, you need to implement a method that returns template XML code. For example:
public XData getInitialPageData(WbdWidget instance)
{
StringBuffer xml = new StringBuffer();
xml.append("\n");
xml.append("\n");
xml.append(" id01\n");
xml.append(" [Label 01]\n");
xml.append(" [Add your widget here 01]\n");
xml.append("\n");
xml.append("\n");
xml.append(" id02\n");
xml.append(" [Label 02]\n");
xml.append(" [Add your widget here 02]\n");
xml.append("\n");
xml.append("");
return new XData(xml);
}
Define a property that defines a name to be displayed above where you enter the XML on the page data tab:
protected void init(WbdWidget instance) throws WbdException
{
instance.defineProperty(new WbdStringProperty("pageDataSection", null, "PageDataSection", ""));
...
}
Use the page data when you are generating the page:
#Override
public void renderForJSP(WbdGenerator generator, WbdWidget instance, UimHelper ud, WbdRenderHelper rh) throws WbdException
{
...
Xpc xpc = ud.getXpc();
xpc.start("tooltwist.wbd.getPagedata", "select");
xpc.attrib("navpointId", WbdSession.getNavpointId(ud.getCredentials()));
xpc.attrib("pageDataSection", pageDataSection);
XData pagedata = xpc.run();
// Do something with the page data
...
}

Display Image From Database in .rdlc report

I have one simple report in which I display company information.
In my sql database I have a CompanyMaster table in which i have one column Company Logo. In that column I only store the path of the company logo image.
Now i want to display company logo in report with company information,
How do I do this?
You could use Image control in rdlc file. Common approach is to write an Webservice that returns the Image content. So your Image URL will point to web service URL.
You webserivce class needs to inherit ImageHandler as follows:
<%# WebHandler Language="C#" Class="ServeImage" %>
using System;
using System.Web;
using System.Drawing.Imaging;
using Microsoft.Web;
using MyControllers;
public class ServeImage : ImageHandler {
public ServeImage()
{
}
public override ImageInfo GenerateImage(System.Collections.Specialized.NameValueCollection parameters)
{
ImageInfo retVal = null;
if (parameters["ID"] != null)
{
MyController myCntl = new MyController();
// myCntl.GetImageFromDB returns the binary[] content from database
retVal = new ImageInfo(myCntl.GetImageFromDB(parameters["ID"]));
}
return retVal;
}
}
Ok.. I think, I should be sorry for making you click on to an another page. Time for steps.
1.) EnableExternaImages= true for your sample report.
2.) Insert an image in your Report. Set the image source as external.
3.) Enter the expression in "use this image".
4.) Enter the value of your image file link that is saved in Database eg . of such expression is : "file:/// " + First(Fields!Imagelink.Value, "DataSet1") + ".jpeg"
5.) Important thing is that your Imagelink value must be the complete path to that image.
Note use "file:///" before the link.
6.) Any more issue please comeback and ask again. :-)
1- Create Text Parameter
2- Add Image Control to the Report
3- Set the Image to be External external and set "Use this image" to be the
parameter: [#ParameterName].
4- Finally Set the Parameter Value in Code behind before loading the report.
Setting image source in RDLC report dynamically

How to display Image from database in Html Helper Web Grid of [MVC 3.0 Razor]

I am working on Web Grid HTMl helper.I want to display data from database.I succeed in displaying two columns from database but dont know hoe to display image from database.I have made the Html helper to display image in database but how to use in web grid HTML helper.
so pls help.
You could create an action that returns an image, then just use the url to it in your image tag:
<img src="/Images/View/123" />
Your action would be something like:
public ActionResult View(string id)
{
return base.File("C:\Path\File", "image/jpeg");
}
I created a small mvc3 app showing the list of Tennis Finalists with a nationality flag next to each player:
For this I used a simple Helper Class:
using System.Web.Mvc;
namespace MvcTennis.Helpers
{
public static class HtmlHelpers
{
public static MvcHtmlString CountryFlag(this HtmlHelper helper, string CountryCode)
{
string sRoot = "~/Content/Images/";
string sFlagPath = sRoot + CountryCode.ToLower() + ".png";
string image = "<img src=\"" + UrlHelper.GenerateContentUrl(sFlagPath, helper.ViewContext.HttpContext ) + "\" width=\"20px;\" border=\"1\" margin=\"0\" >";
MvcHtmlString sHtml = new MvcHtmlString(image);
return sHtml;
}
}
}
The Content/Images folder contains the flag files in .png format.
On the view page (Index.cshtml) we need to add the using directive :
Html/Razor Syntax: