[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Automatic file download in c#

Posted on 2004-10-01
6
Medium Priority
?
1,423 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 800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 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