Response.TransmitFile and delete it after transmission

前端 未结 2 1624
死守一世寂寞
死守一世寂寞 2020-12-19 04:40

I have to implement GEDCOM export in my site.

My .net code created one file at server when export to gedcom clicked.

Then I need to download it to client fro

相关标签:
2条回答
  • 2020-12-19 05:18

    Anything you put after Response.End won't get executed because it throws a ThreadAbortException to stop execution of the page at that point.

    Try this instead:

    string responseFile = Server.MapPath("~/" + FileName);
    
    try{
        Response.ContentType = "text/xml";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.TransmitFile(responseFile);
        Response.Flush();
    }
    finally {
        File.Delete(responseFile);
    }
    
    0 讨论(0)
  • 2020-12-19 05:23

    If the file is reasonably small, you can load it into a byte array so that you can delete the file while still being able to send the data:

    Response.ContentType = "text/xml";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
    string path = Server.MapPath("~/" + FileName);
    byte[] data = File.ReadAllBytes(path);
    File.Delete(path);
    Response.BinaryWrite(data);
    Response.End();
    
    0 讨论(0)
提交回复
热议问题