Solved

Automatic file download in c#

Posted on 2004-10-01
6
1,412 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
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!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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,…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

16 Experts available now in Live!

Get 1:1 Help Now