Open any file from asp.net

前端 未结 2 1303
旧巷少年郎
旧巷少年郎 2021-01-19 23:11

How can I open any file, specified by a path, in ASP.NET programatically?

I tried the snippet below but it reads the contents of the file instead of opening the file

2条回答
  •  伪装坚强ぢ
    2021-01-20 00:06

    You can Response.Redirect to file if you're just opeining it

    or if file is being downloaded you can use the folling code;

        public void DownloadFile(string fileName)
        {
            Response.Clear();
            Response.ContentType = @"application\octet-stream";
            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath(FileName));
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.Flush();
        }
    

提交回复
热议问题