I have to download image, pdf or docs file in mvc3 - asp.net-mvc

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

Related

how doI display image from images file within my wwwroot on my cshtml

The src route is correct. Instead of the image, I get the default 'green hill blue sky' file image on my cshtml
<img src="../../wwwroot/images/images.jpg" width="30" height="30" alt="">
What I tried:
changing it from .jpg to .png
got the same results
Controller that loads the page:
basically, all it does is checks if a user is logged on or not by checking the session. Unsure if I should be doing anything else to also display the images. fairly new to this framework.
[HttpGet]
[Route("dashboard")]
public IActionResult Dashboard()
{
if(HttpContext.Session.GetInt32("UserId") == null)
{
return RedirectToAction("Index","Home");
}
ViewBag.loggedinUser = HttpContext.Session.GetInt32("UserId");
HttpContext.Session.SetInt32("DashboardTab", 0);
return View("Dashboard");
}
The issue is the link to your image.
Try ~ instead of "wwwroot"
Firstly,check if you have app.UseStaticFiles(); in your Startup.cs.
Then try to use
<img src="~/images/images.jpg" width="30" height="30" alt="">
the tilde character ~/ points to the wwwroot.Refer to the official doc.

Image tag is not showing image while image url is fine. MVC

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="" />

Appium: App Path only works with a direct link?

I'm using Laravel php and Appium.
I want Appium to download the .apk/.ipa file from a certain route which returns the downloaded file.
App path in Appium: localhost/downloadApp
public function downloadApp(Request $request) {
...
return response()->download($path);
}
If I try this way, it doesn't work and I get an error "[Support] Error: Plist file doesn't exist: '.../Info.plist". I don't know why because if I call localhost/downloadApp in my browser, it downloads the file.
But if I use the direct link (http://localhost/uploads/HelloWorld.ipa) in Appium, it works.
The path must point to an actual file.
You can download the file itself from your code but you need to make sure that:
The file has completed to download before you set up the driver
The path to the downloaded file is correct, make sure the name of the file and the download location is the same as what you set in the capabilities ("localhost/downloadApp" doesn't seem like the correct download path)
I have the same problem, I just use return redirect ($file_path); and that works fine for apk file. For security, I also use the POST method to start download. This is my code:
//blade code :
<li class="nav-item">
<form method="POST" action="{{url('/files/download')}}">
{{ csrf_field()}}
<button class="btn btn-primary fa fa-download"> download </button>
</form>
</li>
//Route code :
Route::post('/files/download', 'FilesController#download');
//Controller code :
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\File;
use App\Http\Controllers\Controller;
class FilesController extends Controller
{
public function download($file)
{
$file_path = 'your folder path/file.apk';
// if your file in public use $path = "/your folder/file.apk"
// if your file is in out of public you may use two points like that: $path = "../your folder/file.apk"
return redirect ($file_path);
}
}

MVC ExtractAssociatedIcon show icon in view

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">

Icons for each file type

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.

Resources