Icons for each file type - asp.net-mvc

In my MVC application, user can upload various type of files (pdf, jpg, txt etc) and I want to show icon specific to each type, currently I am using one icon (pdf) for the purpose, I want, that based on the file type, associated icon should be displayed. Please guide me what kind of approach I can use for this purpose
<td>
#if (item.CalibratedBy != null){
<a href = #Url.Action("ViewCertificate", new { fileName = item.CalibrationCert }) >
<img src = "#Url.Content("~/Content/Icons/pdf.jpg")" alt = "attachment" /> </a>
}
</td>

Expose the extension type needed as a property on item (presumably being passed in through the Model)
Create an HtmlHelper extension method that will take in item.FileType and return the path to the appropriate icon file to be used. Call this in your <img> tag.

Related

Image doesn't show even when I can see it doing inspect in the browser

I have tried for more than two hours and I can not find an answer to this problem. I have this part of the code in a view strongly typed in MVC:
<td>
#foreach (var TitleBook in Directory.GetFiles(Server.MapPath("~/App_Data/Images"), "*.jpg"))
{
var fileName = Path.GetFileName(TitleBook);
if (Convert.ToInt32(fileName.Substring(0,3)) == item.IdBook)
{
<img src="#TitleBook" alt="Alternate Text" height="100" width="100">
}
}
</td>
The name of the image file is created so the three first characters in the filename are numbers.
When I run the code, I get the alternate message for the image in the hmtl view. However, the path to this image is well read and I know it because I can obtain it by doing inspect in the browser and calling only the src part of in another window which shows the image as expected (sorry for my English). The css is handled by the version of boostrap installed in VS2017.
Could someone point to my error here?
Your titleBook is a full physical path. But <img> wants an URL.
Untested:
<img src="~/App_Data/Images/#fileName" alt="Alternate Text" height="100" width="100">
BTW: with Directory.GetFiles(..., $"{item.IdBook:D3}*.jpg")) you wouldn't need the foreach loop.

Randomly selecting an image from a folder in Umbraco

I have created a folder in Umbraco and I would like to display a random image from that folder on my page.
I found a solution from a few years ago which was giving me compile errors
dynamic folder = Library.MediaById(1054);
var randomImage = folder.Children.Where("nodeTypeAlias = \"Image\"").Random();
I found that I have to add the proper inherits in my file
#using umbraco.MacroEngines
#inherits DynamicNodeContext
but this it gives me an error because I already have a #model I'm using in the first line
The 'inherits' keyword is not allowed when a 'model' keyword is used.
Thanks for any help
You can take a look and learn from sample macro partial views pre-installed in each Umbraco website. There is a snippet called List Images From Media Folder.
The code of the default snippet looks like this:
#inherits Umbraco.Web.Macros.PartialViewMacroPage
#*
Macro to display a series of images from a media folder.
How it works:
- Confirm the macro parameter has been passed in with a value
- Loop through all the media Id's passed in (might be a single item, might be many)
- Display any individual images, as well as any folders of images
Macro Parameters To Create, for this macro to work:
Alias:mediaId Name:Select folder with images Type:Single Media Picker
*#
#{ var mediaId = Model.MacroParameters["mediaId"]; }
#if (mediaId != null)
{
#* Get all the media item associated with the id passed in *#
var media = Umbraco.Media(mediaId);
var selection = media.Children("Image");
if (selection.Any())
{
<ul>
#foreach (var item in selection)
{
<li>
<img src="#item.umbracoFile" alt="#item.Name" />
</li>
}
</ul>
}
}
We need to remember to add the parameter to macro if we're using them there.
Then we can use both: Media or TypedMedia helper methods to retrieve folder (which is typical media item with different type), despite of the required returned type. I usually use TypedMedia to be able to operate on strongly typed object and preview properties in Visual Studio.
If we create macro properly and insert it on the template using code like this (with proper folder ID):
#Umbraco.RenderMacro("Test", new { mediaId="1082" })
we should see a list of images from this folder (all of them at that moment).
The final part is almost the same as you previously did it, but we need to adjust it a little bit. My final code and suggestion is below:
#inherits Umbraco.Web.Macros.PartialViewMacroPage
#{
var folderId = Model.MacroParameters["mediaId"];
if (folderId != null)
{
var media = Umbraco.TypedMedia(folderId);
var rand = new Random();
var imagesInFolder = media.Children("Image");
if(imagesInFolder.Any()) {
var pick = imagesInFolder.ElementAt(rand.Next(0, imagesInFolder.Count()));
if (pick != null)
{
<img src="#pick.GetCropUrl()" alt="#pick.Name" />
}
}
}
}
Let me know if it solved your problem :)

Grails- How to display the last uploaded image?

I used to displayed more than one picture, hence I used the each. Now I have to display only the last uploaded picture. How should I changed my code?
<g:each in ="${statusMessage?.fetchProductPictureUrls() }" var="picture">
<div class="feed-picture">
<div class="fl">
<img class="single" src="${picture }" alt="Product Picture">
</div>
</div>
</g:each>
You can do this by many ways, here are 2:
1 - Filter the desired image in your controller, service or presenter and in your .gsp you just need to access your image variable.
2 - Use a tagLib to do this (not tested):
class LastImageTagLib {
static namespace = "last"
def image = { attr ->
//filter your image
def lastImage = attr.images.last()
out << "<img class='single' src='${lastImage}' alt='Product Picture'>"
}
}
And in your .gsp:
<div class="feed-picture">
<div class="fl">
<last:image images="${statusMessage?.fetchProductPictureUrls()}" />
</div>
</div>
I think the first option is better.
So going on the code above I am unsure if the list "fetchProductPicturesUrls" is sorted or not.
If it is, you have a few options.
You can grab the last entry of the list by leveraging the .last() method.
http://groovy.codehaus.org/groovy-jdk/java/util/List.html
OR
You can grab the list size and track the count by setting the status flag in your foreach loop.
http://grails.org/doc/latest/ref/Tags/each.html
Suggestion:
I would recommend storing images on disk or in the cloud vs storing them in the database.
A nice way to maintain these images is to create a domain. Here is a sample I use for S3 images.
class S3Image {
Date dateCreated
Date lastUpdated
String imageUrl
String imageName
static constraints = {
imageUrl(blank:false)
imageName(blank:false)
}
}

I have to download image, pdf or docs file in mvc3

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);
}

Uploading a document and sending within XForm

I'm using EPiServer CMS 7.5 MVC application.
I can only see textboxes and buttons while creating a new form. I would like to have a link, which uploads a document, when clicked. Then this document should be able to view while looking to form data and also this should be attached along with the mail.
Any help?
There is no file upload control in XForm editor. One option - modify how XForm is rendered. XForm in EPiServer uses display templates to render. One way how to add file upload is to create your own XForm display template and add file upload. Display template will be used for all XForms in your application.
To create display template, create XForm.cshtml under /Views/Shared/DisplayTemplates/ in Visual Studio. Here is sample of source code of XForm.cshtml:
#using EPiServer.HtmlParsing
#using EPiServer.Web.Mvc.Html
#model EPiServer.XForms.XForm
#if (ViewData["XFormActionResult"] is EPiServer.Web.Mvc.XForms.XFormSuccessActionResult)
{
<strong>Form posted.</strong>
}
else
{
using (Html.BeginXForm(Model, new { #class = "form xform" }))
{
if (Model != null)
{
foreach (HtmlFragment fragment in (IEnumerable<HtmlFragment>)ViewData["XFormFragments"] ?? Model.CreateHtmlFragments())
{
// here can override particular fragment
// for example, check if TextBox Css class is "file-upload"
// then replace it with file upload
#Html.Fragment(fragment)
}
}
}
}
After that you have to handle posting the form yourself. This article describes well how to do it: http://www.eyecatch.no/blog/2013/01/using-xforms-and-mvc-in-an-episerver-7-block/
Then on OnActionExecuting in BasePageController you can handle file uploading. You can store it in the blob (in EPi 7 VPP) and store reference (GUID) in the XForm.

Resources