开发者

I made csv file on the server - how can I let the client download this file?

I have made csv on the server like this:

string Expath = @"d:\A.csv";
protected void Button3_Click(object sender, EventArgs e)
{
    FileStream FS = null;
    StreamWriter SW = null;
    try
    {
        FS = new FileStream(Expath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
        SW = new StreamWriter(FS, Encoding.Default);
        SW.WriteLine("Test");
        SW.WriteLine("1,2,3,4,5");
    }
    catch { }
    finally
    {
        if (SW != null)
        {
            SW.Close();
            FS.Close();
        }
    }
}

is it correct to make this file on d: on the server? if not where is better to place hem?

how do to that the client can download this f开发者_开发百科ile after he made it? (asp.net C#)


You could use this code to push the file to browser.

     private void PushToBrowser(string FilePath, byte[] Data)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.BufferOutput = true;
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ContentType = @"application/csv";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(FilePath));
        HttpContext.Current.Response.OutputStream.Write(Data, 0, Data.Length);
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.Close();

        File.Delete(FilePath);
    }

You could use the following code to convert you text data to byte array

byte[] Data = File.ReadAllBytes(FilePath)

I won't recommended keeping generated files outside the web directory, even though its possible to write to a directory outside the web root in ASP.NET through impersonation, but its not at all recommended on production enviornment.


Probably d:\ is not a so good place. If you have a web server running on that server, you have to see where the Virtual Directory of the site in which you intend publish the file is located, and then put the file somewhere here.


When I have made files on the server, eg images I have saved to

Server.MapPath(@"~\" + sFilename)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜