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 8706175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:33:01+00:00 2026-06-13T03:33:01+00:00

I produced a text file and is saved to a location in the project

  • 0

I produced a text file and is saved to a location in the project folder.
How do I redirect them to the url that contains that text file, so they can download the text file.

CreateCSVFile creates the csv file to a file path based on a datatable.

Calling:

string pth = ("C:\\Work\\PG\\AI Handheld Website\\AI Handheld Website\\Reports\\Files\\report.txt");
CreateCSVFile(data, pth); 

And the function:

public void CreateCSVFile(DataTable dt, string strFilePath)
{

    StreamWriter sw = new StreamWriter(strFilePath, false);
    int iColCount = dt.Columns.Count;

    for (int i = 0; i < iColCount; i++)
    {
        sw.Write(dt.Columns[i]);

        if (i < iColCount - 1)
        {
            sw.Write(",");
        }
    }

    sw.Write(sw.NewLine);
    // Now write all the rows.
    foreach (DataRow dr in dt.Rows)
    {
        for (int i = 0; i < iColCount; i++)
        {
            if (!Convert.IsDBNull(dr[i]))
            {
                sw.Write(dr[i].ToString());
            }

            if (i < iColCount - 1)
            {
                sw.Write(",");
            }
        }
        sw.Write(sw.NewLine);
    }
    sw.Close();
    Response.WriteFile(strFilePath);
    FileInfo fileInfo = new FileInfo(strFilePath);

    if (fileInfo.Exists)
    {
        //Response.Clear();
        //Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
        //Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        //Response.ContentType = "application/octet-stream";
        //Response.Flush();
        //Response.TransmitFile(fileInfo.FullName);

    }
}
  • 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-06-13T03:33:02+00:00Added an answer on June 13, 2026 at 3:33 am

    After sw.Close(); just Response.Redirect() them to the URL of the text file. You have to get the URL though, not the path on your physical hard drive. Based on your code you can do the following:

    Uri appRootUri = new Uri(HttpContext.Current.Request.ApplicationPath);
    Uri fileUri = new Uri(Path.GetFullPath(strFilePath));
    
    Uri relativeUri = fileUri.MakeRelativeUri(appRootUri);
    
    Response.Redirect("~/" + relativeUri.ToString());
    

    As suggested by aquinas in the comments, you should really be going the other way though. You should know the relative path to where you want to store the file, in which case you can do it this way instead:

    // If you want to store it at http://yourapplication/files/file.txt
    string relativeFilePath = "/files/file.txt";
    
    CreateCSVFile(dt, Server.MapPath(relativeFilePath));
    Response.Redirect("~" + relativeFilePath);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I encountered a warning produced when compiling my kernel module that I can't get
I am trying to run a ruby script that parses a text file with
I have a text file that looks like this: STUFF UP HERE APEXED NUMBER
I know that Sublime Text 2 can delete the trailing white space on files
I have five text files that are produced by an R program, (each of
I am debugging optimization routine. I produce a log text file where I track
The final images produced by compliers contain both bin file and extended loader format
I have produced a new Maven Project from gae-archetype-gwt from within IntelliJ, and everything
I have some data (produced by a legacy application) that I know is invalid
What is the easiest way to produce a Mach-O object file that does not

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.