splash27
splash27

Reputation: 2107

How to generate txt file then force download in ASP.NET Web Forms?

I need to set a handler to <asp:Button> element. When user clicks, txt file must be generated in code-behind and must be automatically downloaded by user. There is no need to get any data from user. All file content will be generated in code behind. For example, how to return to user a txt file with content of this variable:

string s = "Some text.\nSecond line.";

Upvotes: 2

Views: 14889

Answers (3)

szuuuken
szuuuken

Reputation: 946

I do have a similar case but a little bit more complex usecase.

I do generate different types of files. I wrote a container class Attachment witch holds the content type and the value of the generated file as an Base64 string.

public class Attachment {
   public string Name {get;set;}
   public string ContentType {get;set;}
   public string Base64 {get;set;}
}

This enables me to serve different file types with the same download method.

protected void DownloadDocumentButton_OnClick(object sender, EventArgs e) {
  ASPxButton button = (ASPxButton) sender;
  int attachmentId = Convert.ToInt32(button.CommandArgument);

  var attachment = mAttachmentService.GenerateAttachment(attachmentId);

  Response.Clear();
  Response.AddHeader("content-disposition", $"attachment; filename={attachment.Name}");
  Response.AddHeader("content-type", attachment.ContentType);
  Response.BinaryWrite(Convert.FromBase64String(attachment.Base64));
  Response.End();
}

Upvotes: 0

Nick Mehrdad Babaki
Nick Mehrdad Babaki

Reputation: 12495

For this kind of job you should create file on the fly. Please see the code:

    protected void Button_Click(object sender, EventArgs e)
    {
        string s = "Some text.\r\nSecond line.";

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=testfile.txt");
        Response.AddHeader("content-type", "text/plain");

        using (StreamWriter writer = new StreamWriter(Response.OutputStream))
        {
            writer.WriteLine(s);
        }
        Response.End();
    }
}

Just note for new line you need to use \r\n instead of only \n or use WriteLine function for each line

Upvotes: 8

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

You can simply generate the file on the server side and then push it down to the user. s would be the content of the File that you will generate.

Creating the File

Creating a new File is as simple as writing the data to it,

// var s that you're having
File.Create(Server.MapPath("~/NewFile.txt")).Close();
File.WriteAllText(Server.MapPath("~/NewFile.txt"), s);

This will create a new file (if doesn't exist) and write the content of the variable s to it.

Pushing it down to the user

You can allow the user to download it, using the following code,

// Get the file path
var file = Server.MapPath("~/NewFile.txt");
// Append headers
Response.AppendHeader("content-disposition", "attachment; filename=NewFile.txt");
// Open/Save dialog
Response.ContentType = "application/octet-stream";
// Push it!
Response.TransmitFile(file);

This will let him have the file you just created.

Upvotes: 1

Related Questions