Anyone familiar with the webclient.downloadfile method method? I'm looking for a way to help two users download files from an internal secure website. Right clicking File Save As, and then browsing to a folder is "too many steps". So, I'm curious what
this method can do to reduce clicks and/or automate the download process.
I absolutely agree your code is meeting basic requirements.
I just made a few assumptions here,
It was just not clear for me if the Suzan wanted to pass the files "trough" from an internal website or not.
That is why I pass the bytes trough form anoter webserver (the secured internal) instead of reading a local file on the webserver.
without the contentdisposition rule most files open automaticly in the browser (as in the comments above).
I also assumed that Suzan might now the difference between Server Side Code and lient side code.
I just put in everything useful, according to her "participant" level I assumed Suzan was able to cut out the code she eventually needed.
According to above interpretation I wrote this code.
Or as an example: We have an intranet with many PDF's and word documents. I never download one. I always open them direct from the browser. (wich is possible when comment out content disposition)
I just wondered if your way (using the WebClient object) would get around the Open/Save/Cancel dialog. When I do PDFs, I usually open them in either an IFrame or new client-side window. It was just good to see another way of doing things. It could come in
handy some day for me, so thank you.
funluckykitt...
Participant
1469 Points
1547 Posts
Familiar with webclient.downloadfile method?
Jan 15, 2010 05:47 PM|LINK
Anyone familiar with the webclient.downloadfile method method? I'm looking for a way to help two users download files from an internal secure website. Right clicking File Save As, and then browsing to a folder is "too many steps". So, I'm curious what this method can do to reduce clicks and/or automate the download process.
funluckykitt...
Participant
1469 Points
1547 Posts
Re: Familiar with webclient.downloadfile method?
Jan 15, 2010 07:15 PM|LINK
Forgot to show that I have the file listed as a hyperlink in a gridview..
<ItemTemplate>
<asp:HyperLink ID="link1" runat="server" NavigateUrl='<%#Bind("FileURL","{0}") %>' Target="_blank">
</asp:HyperLink>
</ItemTemplate>
Looking for fewest clicks possible to download and open the file
Caspar Kleij...
Contributor
2680 Points
447 Posts
Re: Familiar with webclient.downloadfile method?
Jan 15, 2010 08:44 PM|LINK
try placing this code in your page, I did not fuzzle with credentials, but if you want to know ask.
the PassDownLoadFile(); must be placed in the top of the page load function
using System; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { PassDownLoadFile(); } private void PassDownLoadFile() { string url = ""; if (Request.QueryString.HasKeys() && Request.QueryString["url"] != null) { url = Server.UrlDecode(Request.QueryString["url"]); // clear the response Response.Clear(); // instansiate a weblient System.Net.WebClient client = new System.Net.WebClient(); // get the filedata byte[] filedata = null; try { filedata = client.DownloadData(url); } catch { // handle errors here } // pass the contenttype Response.AddHeader("CONTENT-TYPE", client.ResponseHeaders["CONTENT-TYPE"]); //grab the filename string filename = Path.GetFileName(url); // content disposition (offer the file as a real download) // if commented out it just opens in browser Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", filename)); //write the rerieved data try { Response.BinaryWrite(filedata); } catch { // handle errors here } // push the response Response.Flush(); // send end Response.End(); } } }I tested it on this url:
<asp:HyperLink ID="link1" runat="server" NavigateUrl='<%#Bind("FileURL","?url={0}") %>' Target="_blank">Caspar Kleijne,
the Netherlands
Caspar Kleij...
Contributor
2680 Points
447 Posts
Re: Familiar with webclient.downloadfile method?
Jan 15, 2010 08:55 PM|LINK
I think you can purge target="_blank" when using contentdisposition
Caspar Kleijne,
the Netherlands
Caspar Kleij...
Contributor
2680 Points
447 Posts
Re: Familiar with webclient.downloadfile method?
Jan 19, 2010 07:26 PM|LINK
But did it work?
Caspar Kleijne,
the Netherlands
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Familiar with webclient.downloadfile method?
Jan 19, 2010 07:57 PM|LINK
Casper, I still get the Open/Save/Cancel dialog, which you also get with this code:
string fileName = @"Images\AG00011_.GIF";
string physicalPath = this.Server.MapPath(fileName);
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(fileName) + "\"");
Response.WriteFile(physicalPath);
Response.Flush();
Response.End();
NC...
Caspar Kleij...
Contributor
2680 Points
447 Posts
Re: Familiar with webclient.downloadfile method?
Jan 19, 2010 08:07 PM|LINK
NC01,
I absolutely agree your code is meeting basic requirements.
I just made a few assumptions here,
According to above interpretation I wrote this code.
Or as an example: We have an intranet with many PDF's and word documents. I never download one. I always open them direct from the browser. (wich is possible when comment out content disposition)
Caspar Kleijne,
the Netherlands
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Familiar with webclient.downloadfile method?
Jan 19, 2010 10:06 PM|LINK
Casper,
I just wondered if your way (using the WebClient object) would get around the Open/Save/Cancel dialog. When I do PDFs, I usually open them in either an IFrame or new client-side window. It was just good to see another way of doing things. It could come in handy some day for me, so thank you.
NC...