Solved

Automatic file download in c#

Posted on 2004-10-01
6
1,415 Views
Last Modified: 2008-01-09
Does anyone have any resources (or code even better) for automatic download of a file (pdf, word, zip etc...) when a form is submitted in c#
Thanks
0
Comment
Question by:jonnyboy69
  • 5
6 Comments
 
LVL 17

Accepted Solution

by:
AerosSaga earned 200 total points
ID: 12200319
private void Page_Load(object sender, System.EventArgs e)
{
 Response.ContentType = "application/zip";
 Response.AddHeader("Content-Length", FileSize.ToString());
 Response.AppendHeader("Content-Disposition", "attachment; filename=myzipfile.zip");
 Response.WriteFile("c:\inetpub\wwwroot\myzipfile.zip");
 Response.Flush();
}

Aeros
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12200330
or

string remoteUri = "http://mcored.2minnoodle.net/downloads/bin/";
string fileName = "test.txt";
string myStringWebResource = null;
WebClient myWebClient = new WebClient();
myStringWebResource = remoteUri + fileName;
myWebClient.DownloadFile(myStringWebResource, fileName);
MessageBox.Show(fileName + " downloaded successfully");
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12200338
or

System.Net.WebClient wc = new System.Net.WebClient();
wc.DownloadFile("http://abstractvb.com/images/Banners/absvb-88x61-xp-02.gif", "c:\button.gif");
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:AerosSaga
ID: 12200358
or

using System;
using System.Net;
using System.Text;
public class DownloadWorker
{
 private long _size;
 private long mRead;
 private DownLoadStatus _status;
 private string _errorDescription;
 private string _sourceURL;
 private string _destPath;
 private string _referer;

 public DownloadWorker()
 {
   base.new();
   _status = DownLoadStatus.Idle;
 }

 public DownloadWorker(string sSourceURL, string sDestPath)
 {
   base.new();
   _sourceURL = sSourceURL;
   _destPath = sDestPath;
 }

 public DownloadWorker(string sSourceURL, string sDestPath, string sReferer)
 {
   base.new();
   _sourceURL = sSourceURL;
   _destPath = sDestPath;
   _referer = sReferer;
 }
 public enum DownLoadStatus
 {
   Idle = 0,
   Connecting = 1,
   Connected = 2,
   Downloading = 3,
   Completed = 4,
   ErrorOccured = 5,
 }
 public event void StatusChanged;
 public event void ProgressChanged;

 public string SourceURL {
   get {
     return _sourceURL;
   }
   set {
     if (_status == DownLoadStatus.Connected || _status == DownLoadStatus.Connecting || _status == DownLoadStatus.Downloading) {
       throw new InvalidOperationException("SourceURL cannot be changed while download is in progress");
     } else {
       _sourceURL = Value;
     }
   }
 }

 public string DestPath {
   get {
     return _destPath;
   }
   set {
     if (_status == DownLoadStatus.Connected || _status == DownLoadStatus.Connecting || _status == DownLoadStatus.Downloading) {
       throw new InvalidOperationException("Destination Path cannot be changed while download is in progress");
     } else {
       _destPath = Value;
     }
   }
 }

 public string Referer {
   get {
     return _referer;
   }
   set {
     if (_status == DownLoadStatus.Connected || _status == DownLoadStatus.Connecting || _status == DownLoadStatus.Downloading) {
       throw new InvalidOperationException("Referer cannot be changed while download is in progress");
     } else {
       _referer = Value;
     }
   }
 }

 public DownLoadStatus Status {
   get {
     return _status;
   }
 }

 public double Progress {
   get {
     if (_size == 0) {
       return 0;
     } else {
       return mRead / _size;
     }
   }
 }

 public long Size {
   get {
     return _size;
   }
 }

 public long Downloaded {
   get {
     return mRead;
   }
 }

 public string ErrorDescription {
   get {
     return _errorDescription;
   }
 }

 private void ChangeStatus(DownLoadStatus NewStatus)
 {
   DownLoadStatus Temp;
   Temp = _status;
   _status = NewStatus;
   if (StatusChanged != null) {
     StatusChanged(this, Temp, NewStatus);
   }
 }

 public void DownloadFile()
 {
   byte[] bBuffer;
   static virtual const int BlockSize = 4096;
   int iRead;
   int iReadTotal;
   int iTotalSize;
   if (_sourceURL == "") {
     throw new InvalidOperationException("No Source URL specified");
     goto exitMethodDeclaration0;
   }
   if (_destPath == "") {
     throw new InvalidOperationException("No Destination Path specified");
     goto exitMethodDeclaration1;
   }
   try {
     ChangeStatus(DownLoadStatus.Connecting);
     HttpWebRequest wr = ((HttpWebRequest)WebRequest.Create(_sourceURL));
     if (_referer != "") {
       wr.Referer = _referer;
     }
     HttpWebResponse resp = ((HttpWebResponse)wr.GetResponse());
     _size = resp.ContentLength;
     ChangeStatus(DownLoadStatus.Connected);
     IO.Stream sIn = resp.GetResponseStream;
     IO.FileStream sOut = new IO.FileStream(_destPath, IO.FileMode.Create);
     ChangeStatus(DownLoadStatus.Downloading);
     iRead = sIn.Read(bBuffer, 0, BlockSize);
     mRead = iRead;
     while (iRead > 0) {
       if (ProgressChanged != null) {
         ProgressChanged(this);
       }
       sOut.Write(bBuffer, 0, iRead);
       iRead = sIn.Read(bBuffer, 0, BlockSize);
       mRead += iRead;
     }
     sIn.Close();
     sOut.Close();
     ChangeStatus(DownLoadStatus.Completed);
   } catch (Exception ex) {
     _errorDescription = ex.Message;
     ChangeStatus(DownLoadStatus.ErrorOccured);
   }
   exitMethodDeclaration1: ;
 }
}
0
 

Author Comment

by:jonnyboy69
ID: 12200374
Nice one I'll give one of them a try. Super quick answering thanks
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12200384
Your very welcome my friend.

Aeros
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

20 Experts available now in Live!

Get 1:1 Help Now