Usuário com melhor resposta
Como fazer um sistema de downloads com o ASP.NET Razor ?

Pergunta
-
Respostas
-
@{ // Armazenar a imagem em cache por um minuto Response.OutputCache(60); var fileId = UrlData[0].AsInt(); var db = Database.Open("PhotoGallery"); var file = db.QuerySingle("SELECT * FROM Files WHERE Id = @0", fileId); if (file == null) { Response.SetStatus(HttpStatusCode.NotFound); return; } string filename = file.FileTitle ; if (filename != "") { string path = Server.MapPath(file.Path); System.IO.FileInfo arquivo = new System.IO.FileInfo(path); if (arquivo.Exists) { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + arquivo.Name); Response.AddHeader("Content-Length", arquivo.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(arquivo.FullName); Response.End(); } else { Response.Write("This file does not exist."); } } }
- Sugerido como Resposta Airton Gomes de Lima domingo, 16 de setembro de 2012 21:56
- Marcado como Resposta welington jrModerator terça-feira, 6 de março de 2018 15:41
Todas as Respostas
-
-
@{ // Armazenar a imagem em cache por um minuto Response.OutputCache(60); var fileId = UrlData[0].AsInt(); var db = Database.Open("PhotoGallery"); var file = db.QuerySingle("SELECT * FROM Files WHERE Id = @0", fileId); if (file == null) { Response.SetStatus(HttpStatusCode.NotFound); return; } string filename = file.FileTitle ; if (filename != "") { string path = Server.MapPath(file.Path); System.IO.FileInfo arquivo = new System.IO.FileInfo(path); if (arquivo.Exists) { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + arquivo.Name); Response.AddHeader("Content-Length", arquivo.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(arquivo.FullName); Response.End(); } else { Response.Write("This file does not exist."); } } }
- Sugerido como Resposta Airton Gomes de Lima domingo, 16 de setembro de 2012 21:56
- Marcado como Resposta welington jrModerator terça-feira, 6 de março de 2018 15:41