3

Currently I have the text file going to desktop, in ASP how can I prompt a file save dialog for the user? The result is string from the streamreader as "result" as follows:

StreamWriter FileWriter = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file.txt"));

                FileWriter.Write(result);

                FileWriter.Close();
2
  • 1
    ASP? Nothing about what you are doing right there is going to save anything to the desktop of the user using a browser. Can you please elaborate more on what you are trying to accomplish? Commented Jul 29, 2010 at 20:35
  • Basically, where do I specify streamwriter to save the file and how do I prompt the user using a save as dialog to save the file? After a link or button is clicked Commented Jul 29, 2010 at 20:44

6 Answers 6

5
String FileName = filename;
String FilePath = filepath;
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath + FileName);
response.Flush();
response.End();
Sign up to request clarification or add additional context in comments.

Comments

2
Response.AppendHeader("Content-Disposition", "attachment");
StreamWriter FileWriter = new StreamWriter(Response.OutputStream);
FileWriter.Write(result);

I didn't try the code, but maybe you need to omit the call to FileWriter.Close() since it will try to dispose the stream. If not, then you should be using using instead. If it's too problematic, write to the stream directly with its Write method or use a MemoryStream.

Comments

2

An example from one of my apps that does it.

    protected void Page_Load(object sender, EventArgs e)
{
    string tempFileName = Request["tempFileName"];  // the temp file to stream
    string attachFileName = Request["attachFileName"];  // the default name for the attached file

    System.IO.FileInfo file = new System.IO.FileInfo(Path.GetTempPath() + tempFileName);
    if (!file.Exists)
    {
        pFileNotFound.Visible = true;
        lblFileName.Text = tempFileName;
    }
    else
    {
        // clear the current output content from the buffer
        Response.Clear();

        // add the header that specifies the default filename for the 
        // Download/SaveAs dialog 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + attachFileName);

        // add the header that specifies the file size, so that the browser
        // can show the download progress
        Response.AddHeader("Content-Length", file.Length.ToString());

        // specify that the response is a stream that cannot be read by the
        // client and must be downloaded
        Response.ContentType = "application/octet-stream";
        // send the file stream to the client
        Response.WriteFile(file.FullName);
    }        
}

1 Comment

You might consider setting the Response.ContentType = "application/octet-stream"; line to the actual mime type of the file, if known.
1
string path = Server.MapPath(your application path);
WebClient client = new WebClient();            
byte[] data = client.DownloadData(new Uri(path));
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", "aspnet.pdf"));
Response.OutputStream.Write(data, 0, data.Length);

try this code.. its helpful

Comments

1

Don't know if its still open but just to help others

Just use this code it should work to prompt the user to open a dialog for opening or saving the file on the system ....

byte[] bytesPDF = System.IO.File.ReadAllBytes(@"C:\sample.pdf");

        if (bytesPDF != null)
        {

            Response.AddHeader("content-disposition", "attachment;filename= DownloadSample.pdf");
            Response.ContentType = "application/octectstream";
            Response.BinaryWrite(bytesPDF);
            Response.End();
        }

Comments

0

If I understand you correctly, you first had a 1-tier (desktop) app that was saving a file using C# to the user's file system. You are now trying to transition to a 2-tier app with your C# code running on the server tier, and the user's browser representing the other tier. There is nothing you can do in C# on the server to write a file to the browser user's file system directly.

That being said, what you need to do is write the file to the HTTP response stream with a content type of something like application/octet-stream. Are you actually using ASP or ASP.NET? If the latter, you should have access to the response stream through a variety of means, but start with the Response object (available from the page, but also available via HttpContext.Current.Response).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.