how to make code execute after Response.end

后端 未结 4 1134
感动是毒
感动是毒 2021-01-03 11:53

My code is like this

HttpContext.Current.Response.Clear();
     HttpContext.Current.Response.ContentType = \"application/pdf\";
     HttpContext.Current.Resp         


        
4条回答
  •  囚心锁ツ
    2021-01-03 12:30

    I had the same issue. try this: copy to MemoryStream -> delete file -> download.

    string absolutePath = "~/your path";
    try {
        //copy to MemoryStream
        MemoryStream ms = new MemoryStream();
        using (FileStream fs = File.OpenRead(Server.MapPath(absolutePath))) 
        { 
            fs.CopyTo(ms); 
        }
    
        //Delete file
        if(File.Exists(Server.MapPath(absolutePath)))
           File.Delete(Server.MapPath(absolutePath))
    
        //Download file
        Response.Clear()
        Response.ContentType = "image/jpg";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + absolutePath + "\"");
        Response.BinaryWrite(ms.ToArray())
    }
    catch {}
    
    Response.End();
    

提交回复
热议问题