Solved

Downloading file via parameter

Posted on 2009-05-13
14
254 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
  • 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now