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

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");
            }
        }
stephenwildeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stephenwildeAuthor Commented:
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
käµfm³d 👽Commented:
Change your application pool from "Classic" mode to "Integrated Pipeline".
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

stephenwildeAuthor Commented:
Googled the term but could not find a suitable way to change settings
0
käµfm³d 👽Commented:
Select your project within Solution Explorer. Then hit F4 to bring up the Properties pane. Change the value of "Managed Pipeline Mode."

Screenshot
0
stephenwildeAuthor Commented:
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
käµfm³d 👽Commented:
...and a web project.
Is it a web site or a web application?
0
stephenwildeAuthor Commented:
website project in .net c#
0
käµfm³d 👽Commented:
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
stephenwildeAuthor Commented:
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
stephenwildeAuthor Commented:
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
stephenwildeAuthor Commented:
Thanks

see my Final post re Add Headers - for solution if using different .net version / IIS
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.