-1

I am using VS2005 c#.

I have this function for converting pipe delimited text files into excel format.

The files will then be stored in the C:\ of the user PC.

However, when I deployed my web application into the test server, the files are saved in the C:\ of the test server instead.

Below is my code snippet, may I know how can I change it to be saved on the users' pc instead of the server's, or if possible, show a popup message for the user to open/save the file. Thank you

if (TextFile.HasFile)
    {
        string strFileName = Server.HtmlEncode(TextFile.FileName);

        string strExtension = Path.GetExtension(strFileName);

            string activeDir = @"C:\";

            string newPath = System.IO.Path.Combine(activeDir, "App_Converted");

            System.IO.Directory.CreateDirectory(newPath);

            string strExcelOutputFilename = "C:/App_Converted/" + xlExtension;

            using (StreamWriter outputWriter = new StreamWriter(File.Create(strExcelOutputFilename)))
            {
                StreamReader inputReader = new StreamReader(TextFile.FileContent);
                while (inputReader.Peek() >= 0)
                {
                    string[] myInputFields = inputReader.ReadLine().Split(new char[] { '|' });
                    List<string> myOutputFields = new List<string>();

                    foreach (string aField in myInputFields)
                    {
                        string oField = aField;
                        if (oField.Contains(","))
                            oField = "\"" + oField + "\"";

                            myOutputFields.Add(oField);
                    }

                    outputWriter.WriteLine(string.Join(",", myOutputFields.ToArray()));

                }
                inputReader.Close();
                UploadStatusLabel.Text = "File stored at [C:/App_Converted/] as file name [namelist.csv]";
                Response.Clear();
                Response.ContentType = "application/vnd.xls";
                Response.AddHeader("Content-Disposition", "attachment;filename=namelist.csv");

                StringWriter swriter = new StringWriter();
                HtmlTextWriter hwriter = new HtmlTextWriter(swriter);

                Response.Write(swriter.ToString());
                Response.End();
                return;
            }

EDIT:

Updated code with save windows. However, I do not know how to link my converted file to the save windows. Need some help here. Above is my updated code.

gymcode
  • 4,431
  • 15
  • 72
  • 128
  • You cannot simply save the file in the user's C drive. Code is executed in the server. File gets saved in the server. – Shoban Feb 17 '12 at 06:31
  • possible duplicate. check this so question [force-download-of-a-file](http://stackoverflow.com/questions/873207/force-download-of-a-file-on-web-server-asp-net-c-sharp) – Nika G. Feb 17 '12 at 06:31
  • 1
    Think about it for a minute - ASP.NET code above runs on the server, so it would make sense that it is stored on the server drive. If this is an intranet app and you are on a LAN you could give your App Pool elevated domain credentials (not advisable) and use a UNC path (\\RemotePC\c$\ ) to save it to the user's PC, however, over the internet you would likely need to ask the user to download the file and store it in the location suggested. – StuartLC Feb 17 '12 at 06:32
  • I would like to change my code to open a popup message like a typical download in Internet Explorer. May I know how do I do it? – gymcode Feb 17 '12 at 06:35

2 Answers2

1

instead of:

    Response.ContentType = "application/vnd.xls";
    Response.AddHeader("Content-Disposition", "attachment;filename=namelist.csv");

    StringWriter swriter = new StringWriter();
    HtmlTextWriter hwriter = new HtmlTextWriter(swriter);

    Response.Write(swriter.ToString());

use this snippet

    Response.ContentType = "application/vnd.xls";
    Response.AddHeader("Content-Disposition", "attachment;filename=namelist.csv");
    string filePath = @"paste a path of your file here";
    Response.WriteFile(filePath);

p.s. and please read the so question that I have posted in the comment. It exactly answers your question and covers many aspects of how to force a download of file from server

Nika G.
  • 2,374
  • 1
  • 17
  • 18
0

You cannot save file to the client machine with the above method. You can either store it in server as you do here and provide (href) link to the client. Or you can write the file as a response back to the browser which will either open it or give options to save. use HttpResponse.TransmitFile

Aravind
  • 4,125
  • 1
  • 28
  • 39
  • I have read through that page, I have updated my code. Need some help with the output of the converted file – gymcode Feb 17 '12 at 06:52
  • You have to set the Response.ContentType http://stackoverflow.com/questions/1155183/alternative-to-response-transmitfile-for-transfering-files-via-http – Aravind Feb 17 '12 at 06:56