2 Replies - 921 Views - Last Post: 14 December 2015 - 11:51 AM

#1 kevin_911  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 130
  • Joined: 02-April 09

Force download of Image file in a browser

Posted 14 December 2015 - 02:45 AM

Hi Guys I am looking for some ideas on how to go about downloading files inside a parent node / parent folder. The files in particular are image files with a same extension (.png).

Basically I am following this tutorial

http://www.aspsnippe...-and-VBNet.aspx

I am able to achieve a tree view structure which is great.

However, I am struggling to understand as how to go about collecting all files inside a Tree View Parent node so that user can download it with a single click. Perhaps loop through the parent node and grab the image files?

I am able to add a click event to the parent node using RootNodeStyle-CssClass and passing a jQuery click function as shown below

$(document).ready(function () {
  $(".rootNodeLevel").click(function () {
    event.stopImmediatePropagation();
    alert("Root Node is clicked");
  });
});



In the code (link provided above) I modified the code for fileNode inside the 'PopulateTreeView' method to wrap the Text inside the span tag and on onclick pass a javascript function to download the image.

//Get all files in the Directory.
      foreach (FileInfo file in directory.GetFiles())
      {
        if (file.Extension.Contains(".png"))
        {
          //Add each file as Child Node.
          TreeNode fileNode = new TreeNode
          {
            //Text = file.Name + "Download",
            Text = string.Format("<span id='childNode' onclick=download(file); nodeVal='{1}'>{0}</span>", file.Name, file.FullName),
            Value = file.FullName,            
            Target = "_blank",
            ImageUrl = "~/App_Themes/Images/icons/image.png",
            NavigateUrl = (new Uri(Server.MapPath("~/"))).MakeRelativeUri(new Uri(file.FullName)).ToString(), 
             
          };          
          directoryNode.ChildNodes.Add(fileNode);
        }        
      }



function download(file) {
      window.location.href = file;
    }



The above just opens the image in a new browser tab instead of downloading the image?

I also came across this post on SOF http://stackoverflow...ad-file-c-sharp the solution is to use response object?

Any suggestions guys?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Force download of Image file in a browser

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 19,878
  • Joined: 05-May 12

Re: Force download of Image file in a browser

Posted 14 December 2015 - 06:30 AM

Moving to ASP.NET...
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Force download of Image file in a browser

Posted 14 December 2015 - 11:51 AM

Quote

The above just opens the image in a new browser tab instead of downloading the image?


That is downloading the image. It also displays the result.

However, I know what you're looking for.

Read this answer:

http://stackoverflow...18674775/694987

Normally, you'd have to handle this server-side, with a header called "Content-Disposition", that tells the browser "this file is to download and save, not open." HTML 5 has provided a new attribute called "download" for links.

So, you would have to change your span to a link. Make the href property the path to the file, and make sure it has a download attribute.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1