用C#下载PDF文件的代码

Sye*_*sir 5 .net c#

我在下载pdf文件时遇到问题.而其他文件则下载.码:

WebClient client = new WebClient();
client.DownloadFile(remoteFilename, localFilename);
Run Code Online (Sandbox Code Playgroud)

如果你知道,请帮助我

Mah*_*hat 8

检查这个方法,希望有所帮助

        public static void DownloadFile(HttpResponse response,string fileRelativePath)
    {
        try
        {
            string contentType = "";
            //Get the physical path to the file.
            string FilePath = HttpContext.Current.Server.MapPath(fileRelativePath);

            string fileExt = Path.GetExtension(fileRelativePath).Split('.')[1].ToLower();

            if (fileExt == "pdf")
            {
                //Set the appropriate ContentType.
                contentType = "Application/pdf";
            }

            //Set the appropriate ContentType.
            response.ContentType = contentType;
            response.AppendHeader("content-disposition", "attachment; filename=" + (new FileInfo(fileRelativePath)).Name);

            //Write the file directly to the HTTP content output stream.
            response.WriteFile(FilePath);
            response.End();
        }
        catch
        {
           //To Do
        }
    }
Run Code Online (Sandbox Code Playgroud)