Solved

Downloading file via parameter

Posted on 2009-05-13
14
291 Views
Last Modified: 2012-05-06
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
Comment
Question by:brettr
[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
  • 6
14 Comments
 
LVL 12

Accepted Solution

by:
GuitarRich earned 300 total points
ID: 24376547
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
 
LVL 39

Expert Comment

by:abel
ID: 24376551
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
 

Author Comment

by:brettr
ID: 24377783
When I hit the DownloadFile line, I keep getting

System.Net.WebException
"An exception occurred during a WebClient request."
0
Technology Partners: 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!

 
LVL 39

Expert Comment

by:abel
ID: 24377917
Do you have a public link of what you are trying to download? Or an example link that contains about the same response?
0
 
LVL 39

Assisted Solution

by:abel
abel earned 200 total points
ID: 24378003
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
 

Author Comment

by:brettr
ID: 24378123
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
 

Author Comment

by:brettr
ID: 24378161
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
 
LVL 39

Expert Comment

by:abel
ID: 24378276
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
 
LVL 39

Expert Comment

by:abel
ID: 24378318
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
 

Author Comment

by:brettr
ID: 24378582
@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
 
LVL 39

Expert Comment

by:abel
ID: 24378797
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
 

Author Closing Comment

by:brettr
ID: 31581036
@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
 
LVL 39

Expert Comment

by:abel
ID: 24380482
(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
 

Author Comment

by:brettr
ID: 24380528
>>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

Technology Partners: 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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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