C# 如何从服务器获取图像或PDF并将其保存在本地计算机中?

C# 如何从服务器获取图像或PDF并将其保存在本地计算机中?,c#,asp.net,C#,Asp.net,获取Pdf/图像: Response.ContentType = "Application/pdf"; Response.AppendHeader("Content-Disposition", "attachment; filename=Demo.pdf"); Response.TransmitFile(Server.MapPath("www.dfdg.com\\Documents\\Documents\\Docnoc1894.pdf")); Response.End(); 这段代码非常接近。

获取Pdf/图像:

Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=Demo.pdf");
Response.TransmitFile(Server.MapPath("www.dfdg.com\\Documents\\Documents\\Docnoc1894.pdf"));
Response.End();

这段代码非常接近。从运行时可访问的文件夹中获取文档非常重要(在我的情况下,AppData工作正常)


这似乎是一种与您当前尝试的方法大不相同的方法,但是,如果您希望从web本地保存文件,下面的代码可能会有所帮助。对不起,如果我不理解也没有回答你的问题

string theFileToDownload = "http://www.dfdg.com/Documents/Documents/Docnoc1894.pdf";
string theSaveToPath = "c:\\temp\\DownloadedFile\\Docnoc1894.pdf";
WebClient wc = new WebClient();
wc.DownloadFile(theFileToDownload, theSaveToPath);
wc.Dispose();

“WebClient请求期间发生异常。如果出现某些错误,则代码将确实引发错误。当前字符串“theFileToDownload”是一个无法解析为任何内容的字符串。因此,首先要用有效的链接替换该链接。然后,您要确保该字符串为”“SaveToPath”“。”“也是一个有效的路径。您将保存文件的文件夹结构必须存在,然后才能将文件下载到该位置。它的本地下载文件方法我正在尝试此方法,但它不起作用。如果知道您遇到了什么错误,我会很有帮助
string theFileToDownload = "http://www.dfdg.com/Documents/Documents/Docnoc1894.pdf";
string theSaveToPath = "c:\\temp\\DownloadedFile\\Docnoc1894.pdf";
WebClient wc = new WebClient();
wc.DownloadFile(theFileToDownload, theSaveToPath);
wc.Dispose();