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>









share|improve this question


























    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>









    share|improve this question
























      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>









      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 19:19









      jason

      1,5522169122




      1,5522169122
























          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>





          share|improve this answer























          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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>





          share|improve this answer























          • 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















          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>





          share|improve this answer























          • 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













          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>





          share|improve this answer














          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>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 using MimeMapping.GetMimeMapping(fileName); method and use that. Refer the GetFile 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












          • 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
















          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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini