c# dynamically rename file upon download request

2019-02-16 16:10发布

Is it possible to rename file when trying to download it? For example, I would like to store files to folders using their id's but when user downloads file I'd like to return the original filename.

2条回答
女痞
2楼-- · 2019-02-16 16:43

just change name of file over here

Response.AppendHeader("Content-Disposition","attachment; filename=LeftCorner.jpg");

for example

 string filename = "orignal file name.ext";
 Response.AppendHeader("Content-Disposition","attachment; filename="+ filename  +"");

Downloading a File with a Save As Dialog in ASP.NET

查看更多
姐就是有狂的资本
3楼-- · 2019-02-16 17:03

nombre = nombre del archivo + extension (ejemplo.txt)

public void DownloadFile(string ubicacion, string nombre)
{
        Response.Clear();
        Response.ContentType = @"application\octet-stream";
        System.IO.FileInfo file = new System.IO.FileInfo(ubicacion);
        Response.AddHeader("Content-Disposition", "attachment; filename=" + nombre);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(file.FullName);
        Response.Flush();
}
查看更多
登录 后发表回答