Usuário com melhor resposta
Dúvida sobre Download

Pergunta
-
Olá para todos, estou com uma dúvida quando ao funcionamento do download.
Para eu disponibilizar um download dinâmico (o conteúdo sempre muda), eu preciso sempre salvar o arquivo no servidor para posteriormente carregá-lo e disponibiliar para download? Não tem como eu criar o arquivo, e sem salvar, com ele ainda na memória, eu disponibilizá-lo para download?
Obrigado.
Respostas
-
Você pode fazer algo do tipo, caso o arquivo esteja fisicamente no disco do servidor:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ARQUIVO.EXTENSAO"));
HttpContext.Current.Response.Charset = string.Empty;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
EnableViewState = false;
HttpContext.Current.Response.WriteFile(Server.MapPath("~/DIRETORIO/ARQUIVO.EXTENSAO"));
HttpContext.Current.Response.Flush();Sendo um array de bytes no banco ou em memória:
public void DownloadFile(byte[] file, string fileName)
{
if (file != null && file.Length > 0 && !string.IsNullOrEmpty(fileName))
{
try
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Concat("attachment;filename=", fileName));
HttpContext.Current.Response.Charset = string.Empty;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
EnableViewState = false;
HttpContext.Current.Response.BinaryWrite(file);
HttpContext.Current.Response.Flush();
}
catch { }
}
}Espero ter ajudado,
Abs,
Rodolfo Paoni
Rodolfo Paoni- Sugerido como Resposta Rodolfo Paoni quinta-feira, 10 de junho de 2010 18:16
- Marcado como Resposta Suity sexta-feira, 11 de junho de 2010 17:51
Todas as Respostas
-
Você pode fazer algo do tipo, caso o arquivo esteja fisicamente no disco do servidor:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ARQUIVO.EXTENSAO"));
HttpContext.Current.Response.Charset = string.Empty;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
EnableViewState = false;
HttpContext.Current.Response.WriteFile(Server.MapPath("~/DIRETORIO/ARQUIVO.EXTENSAO"));
HttpContext.Current.Response.Flush();Sendo um array de bytes no banco ou em memória:
public void DownloadFile(byte[] file, string fileName)
{
if (file != null && file.Length > 0 && !string.IsNullOrEmpty(fileName))
{
try
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Concat("attachment;filename=", fileName));
HttpContext.Current.Response.Charset = string.Empty;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
EnableViewState = false;
HttpContext.Current.Response.BinaryWrite(file);
HttpContext.Current.Response.Flush();
}
catch { }
}
}Espero ter ajudado,
Abs,
Rodolfo Paoni
Rodolfo Paoni- Sugerido como Resposta Rodolfo Paoni quinta-feira, 10 de junho de 2010 18:16
- Marcado como Resposta Suity sexta-feira, 11 de junho de 2010 17:51
-
-
-