How to delete file after download with ASP.NET MVC?

后端 未结 13 1017
执念已碎
执念已碎 2020-12-07 20:51

I want to delete a file immediately after download, how do I do it? I\'ve tried to subclass FilePathResult and override the WriteFile method where

13条回答
  •  抹茶落季
    2020-12-07 21:44

    I have posted this solution in https://stackoverflow.com/a/43561635/1726296

    public ActionResult GetFile()
        {
                string theFilename = ""; //Your actual file name
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment; filename="); //optional if you want forced download
                Response.ContentType = "application/octet-stream"; //Appropriate content type based of file type
                Response.WriteFile(theFilename); //Write file to response
                Response.Flush(); //Flush contents
                Response.End(); //Complete the response
                System.IO.File.Delete(theFilename); //Delete your local file
    
                return new EmptyResult(); //return empty action result
        }
    

提交回复
热议问题