[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

Downloading file via parameter

In ASP.NET 3.5, how do I access a URL like this:

mysite.com/download.aspx?key=444ka6u7

and have it return a particular file, such as somesong.mp3.  Otherwise, it returns 404...or some other status code I can check (denied?).
0
brettr
Asked:
brettr
  • 7
  • 6
2 Solutions
 
GuitarRichCommented:
if the file is a physical file can you check for the key's existance and do Response.Redirect to the mp3 file?
Then if the key does not exists show a custom error message?
0
 
abelCommented:
You mean you want to download something that is pointed to by that link? I.e., if you would copy and paste that link in the browser, the browser would download the data? And you want to do the same from the server side?

If you want to download a file and save it locally, you can do the following:

WebClient Client = new WebClient ();
Client.DownloadFile("http://mysite.com/download.aspx?key=444ka6u7", "localname.mp3");

Open in new window

0
 
brettrAuthor Commented:
When I hit the DownloadFile line, I keep getting

System.Net.WebException
"An exception occurred during a WebClient request."
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
abelCommented:
Do you have a public link of what you are trying to download? Or an example link that contains about the same response?
0
 
abelCommented:
I just tried the following and that works perfectly for me, it downloads googles main page into localname.html in the directory d:\temp.

// download googles main page
WebClient Client = new WebClient();
Client.DownloadFile("http://www.google.com", "d:/temp/localname.html");

Open in new window

0
 
brettrAuthor Commented:
I'm trying it locally in VS.NET.  Your code does work but why when I use:

http://localhost:4002/Default.aspx

for the address parameter, It throws an exception?
0
 
brettrAuthor Commented:
GuitarRich makes a good point.  But Server.Transfer() works better for my case.  Unless there is a complelling reason to use Webclient, I'll stay with Server.Transfer(), which without all of the hassle.
0
 
abelCommented:
Well, if you can use Server.Transfer,you should actually consider Response.Redirect, which is usually a better idea unless you have a real reason for sticking with Transfer (i.e., transfering to another server side page including all the post data).

Your question was about downloading a file. Apparently I misunderstood. Response.Redirect does not download a file for you, it redirects the request to another resource. WebClient can be used for downloading a file locally and storing it, manipulating it or do whatever.
0
 
abelCommented:
situation one
I.e., perhaps you want to show http://my.example.com/mypage.aspx to the user and you want to download a file, located at http://somewhereelse.example.com/otherfile.mp3, and present that to the user without him changing locations (i.e., another link in the address bar), then you should use WebClient.

situation two
In most other cases, you should use a link to that file, to inform the user he's going to another location (the user will see the location on hovering).

situation three
In some rare other cases, you can use Response.Redirect. I.e., when you first need to process some information and based on that you need to show another location, but most of the time, this information is already there and can be used on the server side to create the correct link.

-- Abel --
0
 
brettrAuthor Commented:
@abel,

I don't want the URL to change since the user never sees the URL.  They are getting the file from a winform.  If they have permission, they can select to download a particular MP3.  All they see is the MP3 name.  I don't want to deal with redirects.  The key=444ka6u7 is only used as a light authentication.  Like I said, Server.Transfer() works fine and Webclient doesn't.
0
 
abelCommented:
Well, that's good news. Then I just totally misunderstood you. That you were on WinForms was new to me. You were posting in ASP.NET so I assumed a website. Here here, that's what I get when assuming too much. Should've first asked what you meant. Apologies for the clutter.
0
 
brettrAuthor Commented:
@abel
Although Webclient.DownloadFile() wasn't what I needed, I gave points because I'd like to learn more about its use (on my own of course).  Still don't understand why it doesn't work for me.
0
 
abelCommented:
(from grading comment)> Still don't understand why it doesn't work for me.
tx for the sharing. I don't mind helping you there. You can setup a new question with the error you get and where you call the webclient from (the winform or the server side, the ASP.NET part).
0
 
brettrAuthor Commented:
>>You can setup a new question with the error you get and where you call the webclient from (the winform or the server side, the ASP.NET part).<<

Cool.  Thanks.
0

Featured Post

Get your problem seen by more experts

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

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now