Solved

Automatic file download in c#

Posted on 2004-10-01
6
1,420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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