Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am developing an windows application using C# Visual Studio 2010 .Net 4. In which one of my task is to download a file to my local folder on submitting an URL. The problem is the given HTTP URL doesn't have a file name in it. The file is generated dynamically and it shows "Save As" dialog box, where I could see the file name.

This is system tray application and runs on auto pilot basis without any user interference. Hence, I need to achieve the task of downloading the file "without knowing its name" :(

Hope my question is clear now !!!
Posted
Updated 21-Apr-11 1:56am
v9
Comments
J a a n s 21-Apr-11 6:13am    
You can also do the same, rt? You can show a file save dialog and save the file there. Also you can get the file type (MIME) and file name from the headers of the HTTP response.
Mohandas_Kulasekaran 21-Apr-11 6:22am    
This is an automated process and will run background without the user's interference. Hence the save as dialog box should not be displayed. Can HTTP response be use in WinForm ????
willempipi 21-Apr-11 6:14am    
Something like speedyshare.com where you get a webpage that saids "you're download will start in a few seconds" ?
Csongi Varro 21-Apr-11 6:15am    
Could you provide a test URL for that?:)

If you requested the file (I assume via the HttpWebRequest or WebClient objects), you should know the name of the file. In any case, just save the file with a temporary name, prompt the user for the name they want to use, and rename the temporary file.
 
Share this answer
 
v2
Comments
Mohandas_Kulasekaran 21-Apr-11 6:26am    
I am using WebClient object where URLs has file names. I have no problem downloading them. But there are few links which don't have file name in it, hence I don't the know the file name unless the save as dialogbox appears.
willempipi 21-Apr-11 6:30am    
This should work with the link you provided. Your HttpWebRequest will be to http://dropshipdirect.com/warehouse/vendor_export_speedup.php?use_link=on&ve_sid=fal9vgtv5hg3yo4ofhiqhiuogmy3dbji and then you simply save the response data to a file.
Sergey Alexandrovich Kryukov 21-Apr-11 14:29pm    
That is correct, my 5.
I provide a full source code if my tool, my method can be easily used in UI as well, please see.
--SA
Check this out, it should get you started with a solution:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dropshipdirect.com/warehouse/vendor_export_speedup.php?use_link=on&ve_sid=fal9vgtv5hg3yo4ofhiqhiuogmy3dbji");
WebResponse response = request.GetResponse();
string containsfilename = response.Headers[1];
 
Share this answer
 
v2
Comments
willempipi 21-Apr-11 8:03am    
Seems like the file has been taken offline... I wanted to try your code but I only receive a redirect.
Mohandas_Kulasekaran 21-Apr-11 8:34am    
Aahhhhhh....... I just noticed that being redirected to another url...

Apart from the URL issue, do you find anywhere I am doing wrong in the coding???
willempipi 21-Apr-11 8:58am    
Can't find anything wrong while looking at the code, but as i'm not a computer you need to properly test and debug your code. If you have another file uploaded, you can do this, then also check out the response.Headers for the filename. Good luck coding, I think you have enough info to make this work.
The obvious solution is to put up a SaveFileDialog. If you want no user interaction, you will have to make up a file name, for example by appending a UID to the path part of the URL, or doing something with the query parameters.

Reading the comments, it seems that you have another issue, which is that the page you're actually looking at is not the file you want to download. You may have to grab that page, look through it for the 'real' link, and make another HTTP request to get the file (and don't forget the Referer header on that one).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Apr-11 14:28pm    
I provided full source code of my tool, please see my solution.
--SA
I have developed a fully-fledged HTTPDowloader with important feature — continue interrupted downloads.
It is a console application (in my opinion, the best approach for such task as it can be easily used in batch), but adaptation for UI will be very easy (always use a separate thread(s) for downloads).

Here is the complete source code:
how to download a file from internet[^].

(Note: a biggest part of this code is dedicated to naming of the output file. Input parameters are URL and output file; if output file name is not specified, it is constructed based on URL in unique and uniform manner; the naming is important for continuation of downloads interrupted during previous run-time.)

—SA
 
Share this answer
 
v2
C#
private void DownloadFile()
       {
           
           wc.DownloadProgressChanged += WcOnDownloadProgressChanged;
           wc.DownloadFileCompleted += WcOnDownloadFileCompleted;
           string downloadUrl = ConfigurationSettings.AppSettings["fileDownloadPath"] ;


           wc.DownloadFileAsync(new Uri(downloadUrl), @"C:\\fileName" );
           handle.WaitOne(); // wait for the async event to complete

       }

       private void WcOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
       {
           if (!e.Cancelled && e.Error == null)
           {
               //async download completed successfully

           }
           handle.Set(); // in both the case let the void main() know that async event had finished so that i can quit
       }

       private void WcOnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
       {
           // handle the progres in case of async
           //e.ProgressPercentage

       }
       private void butsync_Click(object sender, EventArgs e)
       {
           DownloadFile();
}

may this help you
 
Share this answer
 
Comments
Bernhard Hiller 4-Apr-14 4:10am    
Do not dump that useless code snippet (useless because of the wc variable - what's that?) to old questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900