I would like to display a file icon in one of my views.
#System.Drawing.Icon.ExtractAssociatedIcon(#"C:/Users/User1/Desktop/test.txt")
Shouldn't this display the icon? Do I need a img tag or something else?
Thanks
You cannot just have the file contents on the browser. You will need to use an <img> tag and have the server return the file stream:
Quick disclaimer: I do not have VS with me, so this is just pseudo-code:
public FileResult Icon()
{
var stream = System.Drawing.Icon.ExtractAssociatedIcon(#"C:/Users/User1/Desktop/test.txt")
return new FileStreamResult(stream, "image/png");
}
Then, just reference the action method:
<img src="/MyController/Icon">
Related
I am new to asp.net MVC. I have the image source in database with the following:
#Model.Image2Url = "C:\Users\vasamad\Documents\VisualStudio2015\Projects\ReactMVC\ReactMVC\Content\Images\Products\3.jpg"
I am adding it to view like this
<img src="#Model.Image2Url" alt="No Image"/>
in browser inspect Element "Could Not load the image"
when I copy this URL and past in new tab its showing the image.
You should store a relative image path to your MVC application root:
~/Content/Images/Products/3.jpg
If you want full image paths then you will need to write a controller action that will read this image from the absolute path and stream it to the response. Finally the <img> tag will need to point to this new controller action. Here's an example:
public ActionResult MyImage()
{
// Get this from your database
string absoluteImagePath = "C:\\Users\\vasamad\\Documents\\VisualStudio2015\\Projects\\ReactMVC\\ReactMVC\\Content\\Images\\Products\\3.jpg";
return File(absoluteImagePath, "image/jpeg");
}
and then in your view point the image tag to this action:
<img src="#Url.Action("MyImage", "MyController")" alt="" />
Please I need some help.
I want to build an online exam web application, so the question files as .rtf stored in database, then when the exam held, us as participant can see the questions by the button list in page.
How I can show/display any .RTF file document (from my database) to page view (.cshtml) ?
public class Exam_Question
{
....
public byte[] Question_File { get; set; }
}
I have stored the file in database.
Then in controller I call the model.
public async Task<IActionResult> Details(int? id)
{
var exam_Question = await _context.Question.SingleOrDefaultAsync(m => m.ID == id);
return View(exam_Question);
}
Then in page view I did try this.
<iframe src="data:#Model.Question_File; base64, #Convert.ToBase64String(Model.Question_File)"></iframe>
the result like this image : IFrame tag with base64 file
I want to display at least at same bold, size, paragraph, equation, etc format like in Word App.
I think one option will be to convert the RTF into HTML at server side and then rendering.
You could either use a paid library as below
SautinSoft
Or build your own RTF to HTML converter. Here is a link to CodeProject which provides pretty detailed breakdown
CodeProject
Sorry I cant think of a simpler solution at the moment
As a variant: Load RTF file and then to open in HTML.
string inpFile = #"..\..\..\..\example.rtf";
string outfile = Path.GetFullPath("Result.html");
RtfToHtml r = new RtfToHtml();
r.Convert(inpFile, outfile, new HtmlFixedSaveOptions() {Title = "Show the RTF." });
This is my code. I have written in MVC3 to download image file, *.pdf file or *.docx file on click.
Variable agreefile defined in foreach loop store path of image file, pdf file and word file. Now I want to download items when user clicks on any item from my view page.
#foreach (string agreefile in Model.SAgreement)
{
<div style="width:100px;height:150px;display:inline-block;margin:10px 5px;">
#*<img src="#agreefile" style="height:150px; width:100px" alt=""/>*#
<object data=#agreefile" type="application/docx" width="300" height="200">
alt : test
</object>
</div>
}
The solution would be to have a button that triggers an action on the controller that returns a file. (you need to change the mime type depending on what you are returning)
Something like this:
public ActionResult GetFile()
{
//...
return File(filePath, "application/pdf", fileDownloadName);
}
I tried to show dynamic urls stored in my database, but the database only have part of the URL, and I tried something like this, but it's not working, any ideas?
#foreach(var item in Model.dominios)
{
url
}
You don,t need Html.DisplayFor() here. Build your url and put it into the link:
#foreach(var item in Model.dominios)
{
var linkUrl = string.Format("http://www.{0}.sss.com", item.subdom);
link text
}
I have a controller as follows that returns a byte[]
public class ImageController : Controller
{
public ActionResult Show(int id)
{
var proxy = new ServiceProxy();
var imgData = proxy.GetCheckImage(id);
return File(imgData, "image/tiff");
}
}
My view is as follows:
<img alt ="" src='#Url.Action("show", "image", new { id = 36 })'/>
I have hard coded the image id for debug purposes.
On the browser in chrome/ie I get a x where the image needs to be displayed. But if I go directly to the controller url http://localhost/website/image/show/id=36, the image gets downloaded fine to the local machine.I have tried creating a separate ActionResult in the same controller which is used to display other data without any luck. This is a Win7/IIS7 local dev. environment.
Tiff is not supported by most browsers. The solution is to convert the Tiff to a Png file.
This post has the solution to your problem:
Render image to the screen from MVC controller
Is Tiff supported by Chrome/IE? I don't think it is...