Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Downloading file via parameter

Posted on 2009-05-13
14
Medium Priority
?
299 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 1200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 800 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

877 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