Click here to Skip to main content
15,922,523 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: how to use image edit in vb. net ? Pin
Dave Kreskowiak25-Apr-07 4:02
mveDave Kreskowiak25-Apr-07 4:02 
GeneralRe: how to use image edit in vb. net ? Pin
babusat25-Apr-07 17:29
babusat25-Apr-07 17:29 
GeneralRe: how to use image edit in vb. net ? Pin
Dave Kreskowiak26-Apr-07 13:39
mveDave Kreskowiak26-Apr-07 13:39 
Questionaccess denied error Pin
alpdoruk24-Apr-07 21:38
alpdoruk24-Apr-07 21:38 
AnswerRe: access denied error Pin
Dave Kreskowiak25-Apr-07 3:59
mveDave Kreskowiak25-Apr-07 3:59 
AnswerRe: access denied error Pin
alpdoruk27-Apr-07 1:23
alpdoruk27-Apr-07 1:23 
Questionhow to specify location of saving file NETCF Pin
laurensia inge24-Apr-07 21:17
laurensia inge24-Apr-07 21:17 
AnswerRe: how to specify location of saving file NETCF Pin
Suhail Shahab24-Apr-07 21:33
Suhail Shahab24-Apr-07 21:33 

Introduction


It is often a common requirement in a web application to have the
ability to download a some sort of file to the clients computer. This article
will illustrate how to create and download a text file to the users computer.


Using the code


Although in the example I actually create the text file before I
stream it out to the client, I feel it is important to highlight that you don't
necessarily have to do this, as the file could actually exist on the file system
and you may want to stream it out to the client. If that is the case you may one
need to use the FileStream to read the already existing document.


We first open the file for reading and we actually read the file byte
for byte into stream, then once we have the file into a stream, we basically
then just use the Response object and download the file via the output
stream.

Response.AddHeader("Content-disposition", "attachment; filename=" + sGenName);
       Response.ContentType = "application/octet-stream";
       Response.BinaryWrite(btFile);
       Response.End();

The real power in this snippet is in the lines above, by adding
header, you are telling the browser to download the file as an attachment. Then
you set The ContentType header which is added, sets your
MIME type so that the browser knows what kind of file it is
about to download. One can choose any of the following MIME types for the
browser.

 ".asf" = "video/x-ms-asf"
 ".avi" = "video/avi"
 ".doc" = "application/msword"
 ".zip" = "application/zip"
 ".xls" = "application/vnd.ms-excel"
 ".gif" = "image/gif"
 ".jpg"= "image/jpeg"
 ".wav" = "audio/wav"
 ".mp3" = "audio/mpeg3"
 ".mpg" "mpeg" = "video/mpeg"
 ".rtf" = "application/rtf"
 ".htm", "html" = "text/html"
 ".asp" = "text/asp"
 
'Handle All Other Files
 = "application/octet-stream"

A full example of how to go about downloading a text file could like
the code below.


C#

Collapse
protected void Button1_Click(object sender, EventArgs e)
  {
      string sFileName = System.IO.Path.GetRandomFileName();
      string sGenName = "Friendly.txt";

      //YOu could omit these lines here as you may not want to save the textfile to the server
      //I have just left them here to demonstrate that you could create the text file
      using (System.IO.StreamWriter SW = new System.IO.StreamWriter(Server.MapPath("TextFiles/" + sFileName + ".txt")))
      {
          SW.WriteLine(txtText.Text);
          SW.Close();
      }

      System.IO.FileStream fs = null;
      fs = System.IO.File.Open(Server.MapPath("TextFiles/" + sFileName + ".txt"), System.IO.FileMode.Open);
      byte[] btFile = new byte[fs.Length];
      fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
      fs.Close();
      Response.AddHeader("Content-disposition", "attachment; filename=" + sGenName);
      Response.ContentType = "application/octet-stream";
      Response.BinaryWrite(btFile);
      Response.End();
  }

VB.NET

Collapse
 Dim strFileName As String = System.IO.Path.GetRandomFileName()
        Dim strFriendlyName As String = "Friendly.txt"

        Using sw As New System.IO.StreamWriter(Server.MapPath("TextFiles/" + strFileName + ".txt"))
            sw.WriteLine(txtText.Text)
            sw.Close()
        End Using

        Dim fs As System.IO.FileStream = Nothing


        fs = System.IO.File.Open(Server.MapPath("TextFiles/" + strFileName + ".txt"), System.IO.FileMode.Open)
        Dim btFile(fs.Length) As Byte
        fs.Read(btFile, 0, fs.Length)
        fs.Close()
With Response
        .AddHeader("Content-disposition", "attachment;filename=" & strFriendlyName)
        .ContentType = "application/octet-stream"
       .BinaryWrite(btFile)
        .End()
end with 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' string sFileName = System.IO.Path.GetRandomFileName();
        Dim sFileName As String
        sFileName = System.IO.Path.GetTempFileName

        ' string sGenName = "Friendly.txt";
        Dim sGenName As String = "Friendly.txt"
        'YOu could omit these lines here as you may not want to save the textfile to the server
        'I have just left them here to demonstrate that you could create the text file 
        'dim  System.IO.StreamWriter SW = new System.IO.StreamWriter(Server.MapPath("TextFiles/" + sFileName + ".txt")))
        Dim sw As System.IO.StreamWriter
        sw = New System.IO.StreamWriter(Server.MapPath("TextFiles/" + sFileName + ".txt"))
        sw.WriteLine("sdfdsfdfsdfsdafasdfasd")
        'SW.WriteLine(txtText.Text);
        sw.Close()


        ' System.IO.FileStream(fs = null)
        Dim fs As System.IO.FileStream
        ' fs = 0
        fs = System.IO.File.Open(Server.MapPath("TextFiles/" + sFileName + ".txt"), System.IO.FileMode.Open)
        ' byte[] btFile = new byte[fs.Length];
        Dim btFile(fs.Length) As Byte

        fs.Read(btFile, 0, Convert.ToInt32(fs.Length))
        fs.Close()
        Response.AddHeader("Content-disposition", "attachment; filename=" + sGenName)
        Response.ContentType = "application/octet-stream"
        Response.BinaryWrite(btFile)
        Response.End()

    End Sub

GeneralRe: how to specify location of saving file NETCF Pin
Dave Kreskowiak25-Apr-07 3:51
mveDave Kreskowiak25-Apr-07 3:51 
GeneralRe: how to specify location of saving file NETCF Pin
Suhail Shahab25-Apr-07 19:08
Suhail Shahab25-Apr-07 19:08 
GeneralRe: how to specify location of saving file NETCF Pin
Suhail Shahab25-Apr-07 19:15
Suhail Shahab25-Apr-07 19:15 
GeneralRe: how to specify location of saving file NETCF Pin
Dave Kreskowiak26-Apr-07 13:41
mveDave Kreskowiak26-Apr-07 13:41 
GeneralRe: how to specify location of saving file NETCF Pin
laurensia inge26-Apr-07 22:36
laurensia inge26-Apr-07 22:36 
Questionhow we hide destop and task Bar in asp.net and vb.net Pin
Suhail Shahab24-Apr-07 21:12
Suhail Shahab24-Apr-07 21:12 
AnswerRe: how we hide destop and task Bar in asp.net and vb.net Pin
Sathesh Sakthivel25-Apr-07 0:23
Sathesh Sakthivel25-Apr-07 0:23 
GeneralRe: how we hide destop and task Bar in asp.net and vb.net Pin
Suhail Shahab25-Apr-07 19:27
Suhail Shahab25-Apr-07 19:27 
AnswerRe: how we hide destop and task Bar in asp.net and vb.net Pin
Dave Kreskowiak25-Apr-07 3:49
mveDave Kreskowiak25-Apr-07 3:49 
GeneralRe: how we hide destop and task Bar in asp.net and vb.net Pin
Suhail Shahab25-Apr-07 19:20
Suhail Shahab25-Apr-07 19:20 
GeneralRe: how we hide destop and task Bar in asp.net and vb.net Pin
Dave Kreskowiak26-Apr-07 13:42
mveDave Kreskowiak26-Apr-07 13:42 
QuestionReports to EXCEL Pin
venkata lakshmi prasanna24-Apr-07 20:04
venkata lakshmi prasanna24-Apr-07 20:04 
QuestionSetting Wrap text for a label in VB.Net Pin
venkata lakshmi prasanna24-Apr-07 20:02
venkata lakshmi prasanna24-Apr-07 20:02 
AnswerRe: Setting Wrap text for a label in VB.Net Pin
MatrixCoder24-Apr-07 20:23
MatrixCoder24-Apr-07 20:23 
AnswerRe: Setting Wrap text for a label in VB.Net Pin
Dave Kreskowiak25-Apr-07 3:46
mveDave Kreskowiak25-Apr-07 3:46 
QuestionSerial port configuration dialog [modified] Pin
Nick Alexeev24-Apr-07 16:22
professionalNick Alexeev24-Apr-07 16:22 
AnswerRe: Serial port configuration dialog Pin
Dave Kreskowiak24-Apr-07 18:02
mveDave Kreskowiak24-Apr-07 18:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.