How to handle a controller result that returns a file in ASP.NET MVC
up vote
0
down vote
favorite
I have a controller that returns a file like this :
byte fileBytes=System.IO.File.ReadAllBytes(Path.Combine(Server.MapPath("~/Images/"), photoPath));
string fileName = prop;
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
I can display it in HTML image like this
<div class="col-md-6"><img src="@("File/Download?id="+ Model.ID+"&prop=photo")" alt="Image" /></div>
I want to create a link using that controller, so that when user clicks it, it will show the image :
<div class="col-md-4"><a @(Model.path == null ? Html.ActionLink("click here", "Download", "File", new { appid = Model.ID.ToString(), prop = "photo" }, null) : Html.ActionLink("none", "", ""))</a></div>
html css asp.net asp.net-mvc razor
add a comment |
up vote
0
down vote
favorite
I have a controller that returns a file like this :
byte fileBytes=System.IO.File.ReadAllBytes(Path.Combine(Server.MapPath("~/Images/"), photoPath));
string fileName = prop;
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
I can display it in HTML image like this
<div class="col-md-6"><img src="@("File/Download?id="+ Model.ID+"&prop=photo")" alt="Image" /></div>
I want to create a link using that controller, so that when user clicks it, it will show the image :
<div class="col-md-4"><a @(Model.path == null ? Html.ActionLink("click here", "Download", "File", new { appid = Model.ID.ToString(), prop = "photo" }, null) : Html.ActionLink("none", "", ""))</a></div>
html css asp.net asp.net-mvc razor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a controller that returns a file like this :
byte fileBytes=System.IO.File.ReadAllBytes(Path.Combine(Server.MapPath("~/Images/"), photoPath));
string fileName = prop;
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
I can display it in HTML image like this
<div class="col-md-6"><img src="@("File/Download?id="+ Model.ID+"&prop=photo")" alt="Image" /></div>
I want to create a link using that controller, so that when user clicks it, it will show the image :
<div class="col-md-4"><a @(Model.path == null ? Html.ActionLink("click here", "Download", "File", new { appid = Model.ID.ToString(), prop = "photo" }, null) : Html.ActionLink("none", "", ""))</a></div>
html css asp.net asp.net-mvc razor
I have a controller that returns a file like this :
byte fileBytes=System.IO.File.ReadAllBytes(Path.Combine(Server.MapPath("~/Images/"), photoPath));
string fileName = prop;
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
I can display it in HTML image like this
<div class="col-md-6"><img src="@("File/Download?id="+ Model.ID+"&prop=photo")" alt="Image" /></div>
I want to create a link using that controller, so that when user clicks it, it will show the image :
<div class="col-md-4"><a @(Model.path == null ? Html.ActionLink("click here", "Download", "File", new { appid = Model.ID.ToString(), prop = "photo" }, null) : Html.ActionLink("none", "", ""))</a></div>
html css asp.net asp.net-mvc razor
html css asp.net asp.net-mvc razor
asked Nov 7 at 19:19
jason
1,5522169122
1,5522169122
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
The Html.ActionLink
helper method will generate HTML markup for the anchor tag. Does not make sense to call it inside the opening a
tag
A simple If
condition can do.
<div class="col-md-4">
@if(Model.path == null)
{
@Html.ActionLink("click here", "Download", "File",
new { appid = Model.ID, prop = "photo" }, null);
}
else
{
<span>Nothing to download or even another link you can use here</span>
}
</div>
It worked but the downloaded file type looks like a generic file. How can I make it specific like .pdf or .jpg? How should I modify the controller? Thanks for the answer.
– jason
Nov 7 at 19:35
You need to fix your action method to send the correct content/mime type.For the image, get the content type usingMimeMapping.GetMimeMapping(fileName);
method and use that. Refer theGetFile
method in this answer
– Shyju
Nov 7 at 19:37
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The Html.ActionLink
helper method will generate HTML markup for the anchor tag. Does not make sense to call it inside the opening a
tag
A simple If
condition can do.
<div class="col-md-4">
@if(Model.path == null)
{
@Html.ActionLink("click here", "Download", "File",
new { appid = Model.ID, prop = "photo" }, null);
}
else
{
<span>Nothing to download or even another link you can use here</span>
}
</div>
It worked but the downloaded file type looks like a generic file. How can I make it specific like .pdf or .jpg? How should I modify the controller? Thanks for the answer.
– jason
Nov 7 at 19:35
You need to fix your action method to send the correct content/mime type.For the image, get the content type usingMimeMapping.GetMimeMapping(fileName);
method and use that. Refer theGetFile
method in this answer
– Shyju
Nov 7 at 19:37
add a comment |
up vote
0
down vote
accepted
The Html.ActionLink
helper method will generate HTML markup for the anchor tag. Does not make sense to call it inside the opening a
tag
A simple If
condition can do.
<div class="col-md-4">
@if(Model.path == null)
{
@Html.ActionLink("click here", "Download", "File",
new { appid = Model.ID, prop = "photo" }, null);
}
else
{
<span>Nothing to download or even another link you can use here</span>
}
</div>
It worked but the downloaded file type looks like a generic file. How can I make it specific like .pdf or .jpg? How should I modify the controller? Thanks for the answer.
– jason
Nov 7 at 19:35
You need to fix your action method to send the correct content/mime type.For the image, get the content type usingMimeMapping.GetMimeMapping(fileName);
method and use that. Refer theGetFile
method in this answer
– Shyju
Nov 7 at 19:37
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The Html.ActionLink
helper method will generate HTML markup for the anchor tag. Does not make sense to call it inside the opening a
tag
A simple If
condition can do.
<div class="col-md-4">
@if(Model.path == null)
{
@Html.ActionLink("click here", "Download", "File",
new { appid = Model.ID, prop = "photo" }, null);
}
else
{
<span>Nothing to download or even another link you can use here</span>
}
</div>
The Html.ActionLink
helper method will generate HTML markup for the anchor tag. Does not make sense to call it inside the opening a
tag
A simple If
condition can do.
<div class="col-md-4">
@if(Model.path == null)
{
@Html.ActionLink("click here", "Download", "File",
new { appid = Model.ID, prop = "photo" }, null);
}
else
{
<span>Nothing to download or even another link you can use here</span>
}
</div>
edited Nov 7 at 19:35
answered Nov 7 at 19:27
Shyju
142k86328433
142k86328433
It worked but the downloaded file type looks like a generic file. How can I make it specific like .pdf or .jpg? How should I modify the controller? Thanks for the answer.
– jason
Nov 7 at 19:35
You need to fix your action method to send the correct content/mime type.For the image, get the content type usingMimeMapping.GetMimeMapping(fileName);
method and use that. Refer theGetFile
method in this answer
– Shyju
Nov 7 at 19:37
add a comment |
It worked but the downloaded file type looks like a generic file. How can I make it specific like .pdf or .jpg? How should I modify the controller? Thanks for the answer.
– jason
Nov 7 at 19:35
You need to fix your action method to send the correct content/mime type.For the image, get the content type usingMimeMapping.GetMimeMapping(fileName);
method and use that. Refer theGetFile
method in this answer
– Shyju
Nov 7 at 19:37
It worked but the downloaded file type looks like a generic file. How can I make it specific like .pdf or .jpg? How should I modify the controller? Thanks for the answer.
– jason
Nov 7 at 19:35
It worked but the downloaded file type looks like a generic file. How can I make it specific like .pdf or .jpg? How should I modify the controller? Thanks for the answer.
– jason
Nov 7 at 19:35
You need to fix your action method to send the correct content/mime type.For the image, get the content type using
MimeMapping.GetMimeMapping(fileName);
method and use that. Refer the GetFile
method in this answer– Shyju
Nov 7 at 19:37
You need to fix your action method to send the correct content/mime type.For the image, get the content type using
MimeMapping.GetMimeMapping(fileName);
method and use that. Refer the GetFile
method in this answer– Shyju
Nov 7 at 19:37
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196340%2fhow-to-handle-a-controller-result-that-returns-a-file-in-asp-net-mvc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown