Skip to main content
 首页 » 编程设计

c#之下载链接失效

2024年02月23日14yjmyzz

我正在使用链接按钮从服务器下载文件。 当我使用页面的完整路径时,它工作正常。

下载链接在默认页面上。

即http://mydomain.com/default.aspx 但在使用 http://mydomain.com/ 时不起作用

当然,它在本地主机上工作,而不是在服务器上工作。

我使用的代码是

   try 
        { 
 
            // System.String filename = this.ViewState["ImageName"].ToString(); 
            string fileName = "filename.pdf"; 
            // set the http content type to "APPLICATION/OCTET-STREAM 
            Response.ContentType = "APPLICATION/OCTET-STREAM"; 
 
            // initialize the http content-disposition header to 
            // indicate a file attachment with the default filename 
            // "myFile.txt" 
            System.String disHeader = "Attachment; Filename=\"" + fileName + 
               "\""; 
            Response.AppendHeader("Content-Disposition", disHeader); 
 
            // transfer the file byte-by-byte to the response object 
 
            System.IO.FileInfo fileToDownload = new 
               System.IO.FileInfo(Server.MapPath("~/UserUploads/") + fileName); 
            Response.Flush(); 
            Response.WriteFile(fileToDownload.FullName); 
        } 
        catch (System.Exception ex) 
        // file IO errors 
        { 
            //SupportClass.WriteStackTrace(ex, Console.Error); 
            throw ex; 
        } 
} 

请帮我解决这个问题

提前致谢

世斌

请您参考如下方法:

试试这个

HttpContext.Current.Request.MapPath("~/" + fileName ); 

Check this for more details