我使用WebClient.DownloadFile将映像下载到本地存储库,如下所示:
WebClient myWC = new WebClient();
myWC.Credentials = new System.Net.NetworkCredential(username, password);
string photoPath = @"\images\Employees\" + employee + ".jpg";
myWC.DownloadFile(userResult[12].Values[0].Value.ToString(), photoPath);我的预期结果如下:我的web应用程序部署在这里:
C:\Inetpub\wwwroot\MyWebApp
我以为这个能把照片保存到
C:\Inetpub\wwwroot\MyWebApp\images\Employees...
相反,我所有的照片都保存在这里:
C:\图片\雇员
我想我并不完全理解DownloadFile方法,因为我觉得路径应该相对于应用程序部署到的目录。如何更改路径,使其相对于应用程序的目录?
注意:,我不想使用物理路径,因为我有一个Dev和QA站点,如果事情被移动,我不希望这些路径中断。
发布于 2011-10-10 17:09:08
在ASP.NET应用程序中,您可以使用Server.MapPath方法映射到相对于站点根目录的物理文件夹(由~表示):
string photoPath = Server.MapPath("~/images/Employees/" + employee + ".jpg");发布于 2013-10-19 09:18:37
photoPath中的前导反斜杠使路径成为从根目录(在示例中为C:\)开始的绝对路径。让它成为一个相对路径,只需删除前面的反斜杠:
string photoPath = @"images\Employees\" + employee + ".jpg";备注: DownloadFile(...)不会为您创建目录。确保它在那里:
Directory.CreateDirectory("images\Employees");发布于 2020-09-23 19:57:19
string photoPath = @"images\Employees\" + employee + ".jpg";在powershell中不工作,并给出以下错误:
At line:1 char:22
+ string photoPath = @"images\Employees\" + employee + ".jpg";
+ ~
No characters are allowed after a here-string header but before the end of the line.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedCharactersAfterHereStringHeaderhttps://stackoverflow.com/questions/7716241
复制相似问题