Solved

Automatic file download in c#

Posted on 2004-10-01
6
1,416 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

815 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

7 Experts available now in Live!

Get 1:1 Help Now