Solved

How do you save web server Image file to Client Hard drive

Posted on 2014-09-21
12
258 Views
Last Modified: 2014-09-22
Trying to allow the client to save an image file from the web server when they are browsing website to their hard drive and choose a file name and where they save it!

but code below doesn't work as don't have permission to save to client disk.

Any ideas for alternative way please?

        protected void ImageBtnDownload_Click(object sender, ImageClickEventArgs e)
        {
            string localFilename = @"c:\testfile.jpg";
            using (WebClient client = new WebClient())
            {
                client.DownloadFile("http://www.yourwebsite.co.uk/images/test small.png","localFilename");
            }
        }
0
Comment
Question by:stephenwilde
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40335315
And that won't work. That code runs on the server, not the client. You're effectively trying to download a file from the server to the server.

What you need to do instead is trigger a download prompt on the client. The way you do that is by specifying a specific set of headers, and changing the response that goes back.

protected void ImageBtnDownload_Click(object sender, ImageClickEventArgs e)
{
    string localFilename = @"c:\testfile.jpg";
    byte[] data = File.ReadAllBytes(localFilename);
    Response.Clear();
    Response.Headers["Content-Type"] = "image/jpeg";
    Response.Headers["Content-Disposition"] = "Content-Disposition: attachment; filename='" + Path.GetFileName(localFilename) + "'";
    Response.BinaryWrite(data);
    Response.Flush();
}

Open in new window

0
 

Author Comment

by:stephenwilde
ID: 40335335
Thank you for code but it failed when run on my development environment (windows and .net visual studio 2010)

Fails on (using png not jpg) ;
Response.Headers["Content-Type"] = "image/png";

with error:
This operation requires IIS integrated pipeline mode.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40335337
Change your application pool from "Classic" mode to "Integrated Pipeline".
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:stephenwilde
ID: 40335434
Googled the term but could not find a suitable way to change settings
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40335512
Select your project within Solution Explorer. Then hit F4 to bring up the Properties pane. Change the value of "Managed Pipeline Mode."

Screenshot
0
 

Author Comment

by:stephenwilde
ID: 40335576
Thanks for going to the trouble of a screen grab

But my f4 brings up Project Properties and first line development server but under it is only one choice:

Always start when debugging

the other choices on your screen shot are not present, so cannot change managed pipeline mode

it is Visual studio 210 and a web project.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40335585
...and a web project.
Is it a web site or a web application?
0
 

Author Comment

by:stephenwilde
ID: 40335593
website project in .net c#
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40335618
I'll have to dig around on this one. I usually don't create web site projects, so I am not too familiar with them (as opposed to web applications). The Integrated Pipeline may not be available for those project types.

Regardless, the headers that I mention above are what you need to include in your response to the client. How you get them in there is the question at hand now.
0
 

Author Comment

by:stephenwilde
ID: 40335628
Thanks

I did transfer code to web server and received the same error;

This operation requires IIS integrated pipeline mode

It is an old Windows server 2005

But not keen to change IIS settings in case it effects other websites hosted on the server.
0
 

Author Comment

by:stephenwilde
ID: 40336097
Thanks for your help - Problem now solved - it is mixing .net 4 and IIS 6

So need to use another way to add Headers code was finally;

  string ImagePath = System.Web.HttpContext.Current.Server.MapPath("~/Images");
            string Filename = "test file small.png";
            string LocalFilename = ImagePath + "\\" + Filename ;
            FileInfo Imagefile = new FileInfo(LocalFilename) ;
            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition","attachment; filename=" + Filename) ;
            Response.AddHeader("Content-Length", Imagefile.Length.ToString());
            Response.AddHeader("Content-Type","application/octet-stream");
            Response.WriteFile(LocalFilename);
            Response.End();

Open in new window

0
 

Author Closing Comment

by:stephenwilde
ID: 40336101
Thanks

see my Final post re Add Headers - for solution if using different .net version / IIS
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 53
Asp.Net Session Question 2 34
auto play video on web page 4 39
Starting to use Git with Visual  Studio Online 1 21
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question