下载链接无效

时间:2012-01-13 08:08:36

标签: c# asp.net download

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

下载链接位于默认页面上。

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;
        }
}

请帮我解决此问题

提前致谢

Shibin

1 个答案:

答案 0 :(得分:4)

试试这个

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

Check this for more details