Asp.Net不仅在客户端创建文本文件,还删除它 [英] Asp.Net not only create a text file on client but also delete it

查看:85
本文介绍了Asp.Net不仅在客户端创建文本文件,还删除它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下按钮时,我必须在客户端上创建一个文本文件,打开它然后将其删除。我可以创建并打开/下载它:



When pressing a button I have to create on client a text file, open it and then delete it. I can create and open/download it with:

protected void btNotepadPending_Click(object sender, EventArgs e)
{
    string sFileName = System.IO.Path.GetRandomFileName();      
    try
    {
        String testo = "XXX" + Environment.NewLine;
        int iii = 0;
        foreach (var item in lUserPending)
            testo += ++iii + " - " + item.Item1 + " " + item.Item2;


        //Create and populate a memorystream 
        MemoryStream mstream = GetTextAsStream(testo);

        //Convert the memorystream to an array of bytes.
        byte[] byteArray = mstream.ToArray();

        //Clean up the memory stream
        mstream.Flush();
        mstream.Close();

        // Clear all content output from the buffer stream
        Response.Clear();

        // Add a HTTP header to the output stream that specifies the default filename
        // for the browser's download dialog
        Response.AddHeader("Content-Disposition", "attachment; filename=delenda.txt");


        // Add a HTTP header to the output stream that contains the
        // content length(File Size). This lets the browser know how much data is being transfered
        Response.AddHeader("Content-Length", byteArray.Length.ToString());


        // Set the HTTP MIME type of the output stream
        Response.ContentType = "application/octet-stream";

        // Write the data out to the client.
        Response.BinaryWrite(byteArray);
        Response.Flush();
        Response.Close();
    }
    catch (Exception exc)
    {
        MessageBox.SendErrorEmail("Exception: " + exc);
    }
}

private MemoryStream GetTextAsStream(String text)
{
    //Create the return memorystream object
    MemoryStream ReturnStream = new MemoryStream();

    //Create a streamwriter to write to the memory stream
    StreamWriter WriteStream = new StreamWriter(ReturnStream);

    //Write the text in the textbox to the Memory Stream.
    WriteStream.WriteLine(text);

    //Clean up the stream writer
    WriteStream.Flush();
    WriteStream.Close();

    //Return the memory Stream
    return ReturnStream;

}





但如何删除它?我试图在退出时删除







but how to delete it? I tried to put a delete when exiting on


protected override void OnUnload(EventArgs e)
{
    if (IsPostBack)
    {
        if (File.Exists("delenda.txt"))
            File.Delete("delenda.txt");
    }
}





但它从未停止过。那么现在我怎么能在记事本(或任何其他相关程序关闭?)



but it never stopped here. So how can I now when notepad (or any other associated program is closed?)

推荐答案

你的代码没有在客户端上创建文件,它提供的是客户端有机会下载该文件。您无法在客户端上创建文件,也无法删除它们。您是否希望网站在文件系统上创建和删除文件?
Your code isn't creating a file on the client, it is offering the client the opportunity to download the file. You can't create files on the client and you can't delete them either. Would you want web sites creating and deleting files on your file system?


这篇关于Asp.Net不仅在客户端创建文本文件,还删除它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆