Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7493967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:15:54+00:00 2026-05-29T17:15:54+00:00

In ASP.Net (with C#) I’m trying to create a .DAT file with plain text

  • 0

In ASP.Net (with C#) I’m trying to create a .DAT file with plain text in it and send it to the browser and force download. I’ve tried several things but I can’t get it working. In my aspx-file there is an ImageButton

<asp:ImageButton ID="btnSave" runat="server" CausesValidation="False" ImageUrl="~/Images/Stages/Database/Save.png" OnClick="btnSave_OnClick" Width="26px" />

In the OnClick-method I’m trying to create the file and send it to the browser.

protected void btnSave_OnClick(object sender, EventArgs e)
{
    string file = "test.dat";
    string fileName = "~\\Stages\\Broekx\\Databanken\\" + file;

    FileStream fs = new FileStream(MapPath(fileName), FileMode.Open);
    long cntBytes = new FileInfo(MapPath(fileName)).Length;
    byte[] byteArray = new byte[Convert.ToInt32(cntBytes)];
    fs.Read(byteArray, 0, Convert.ToInt32(cntBytes));
    fs.Close();

    ImageButton btnSave = (ImageButton)FormViewStagesDummy.FindControl("btnSave");
    btnSave.Visible = false;

    File.Delete(Server.MapPath(fileName));

    if (byteArray != null)
    {
        this.Response.Clear();
        this.Response.ContentType = "text/plain";
        this.Response.AddHeader("content-disposition", "attachment;filename=" + file);
        this.Response.BinaryWrite(byteArray);
        this.Response.End();
        this.Response.Flush();
        this.Response.Close();
    }
}

The file test.dat exists in the correct folder and has to be deleted after it has been read into bytes. I’ve tried this without deleting the file and that wont work either.

After clicking btnSave the button has to be hidden, so that’s why I set the parameter Visible to false.

I’ve also tried it with content-type “application/octet-stream” or with a PDF file and content-type “application/pdf” but nothing works. The page loads normally and no file is being downloaded.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:15:56+00:00Added an answer on May 29, 2026 at 5:15 pm

    Is the file string’s path actually correct?

    this.Response.AddHeader("content-disposition", "attachment;filename=" + file);
    

    Should it not be filename?

    Why are you deleting the file before it is written to the response? Would it not make more sense to serve the file via the response and then delete it?

    i.e. call

    File.Delete(Server.MapPath(fileName));
    

    after the repsonse.

    You should try:

    Response.TransmitFile( Server.MapPath(fileName) );
    Response.End();
    

    TransmitFile is very efficient because it basically offloads the file streaming to IIS including potentially causing the file to get cached in the Kernal cache (based on IIS’s caching rules).
    Response.End();

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ASP.Net FileUpload not working in google chrome.It shows validation error,even after choosing right file
ASP.NET MVC Model Binding is still new to me and I'm trying to understand
ASP.NET has four different types of file results: FileContentResult: Sends the contents of a
ASP.NET newbie here. I would like to create a form with multiple types of
Asp.net A.aspx I'm using JQuery to access an ashx file which loads control (
[ASP.NET 4.0 / EF 4.1] Hi, I´m trying to use "Linq Methods" to filter
ASP.NET MVC makes it really easy to create editing templates for simple, flat objects.
ASP.NET is great for creating html. We send out a lot of html email
ASP.NET MVC has a bunch of custom item templates to create controllers, views, etc.
ASP.Net has stock classes for Roles and Memberships. We can use aspnet_regsql.exe to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.