简体   繁体   English

c# - 给定物理路径,Server.MapPath 中需要虚拟路径

[英]c# - Physical path given, virtual path expected in Server.MapPath

I trying to change my directory which in my local c disk, but where errors says in the title.我试图更改我本地 c 磁盘中的目录,但标题中显示错误。 Is there any way aside from using Server.MapPath?.除了使用 Server.MapPath 之外还有什么办法吗? I'm using a ZipOutputStream nuget package.我正在使用 ZipOutputStream nuget package。

I want to locate my directory in C: instead inside the project folder.我想在 C 中找到我的目录:而不是在项目文件夹中。

 public FileResult DownloadZipFileSig(string FileId){ 
                   
  var fileName = "FilesDL".zip";
  var tempOutPutPath = Server.MapPath(Url.Content("C:/Users/SDILAP2/Desktop/ID_Esig_Files")) + fileName;
              
            
                     using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(tempOutPutPath)))
                     {
                        s.SetLevel(9); 
            
                        byte[] buffer = new byte[4096];
            
                        List<string> stringList = FileId.Split(',').ToList();
            
                        List<string> tempList = new List<string>();
            
                        foreach (string str in stringList)
                        {
            
                           if (System.IO.File.Exists(Server.MapPath("C:/Users/SDILAP2/Desktop/ID_Esig_Files/" + str + ".jpeg")))
                           {
                              tempList.Add(Server.MapPath("C:/Users/SDILAP2/Desktop/ID_Esig_Files/" + str + ".jpeg"));
            
                           }
          
            
                        }
            
                        stringList = tempList;
            
            
                        for (int i = 0; i < stringList.Count; i++)
                        {
                           ZipEntry entry = new ZipEntry(Path.GetFileName(stringList[i]));
                           entry.DateTime = DateTime.Now;
                           entry.IsUnicodeText = true;
                           s.PutNextEntry(entry);
            
                           using (FileStream fs = System.IO.File.OpenRead(stringList[i]))
                           {
                              int sourceBytes;
                              do
                              {
                                 sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                 s.Write(buffer, 0, sourceBytes);
                              } while (sourceBytes > 0);
                           }
                        }
                        s.Finish();
                        s.Flush();
                        s.Close();
            
                     }
            
                     return File(finalResult, "application/zip", fileName);
            
                  }

You might be not quite grasping how web URL's work, and how server.mappath() is to be used.您可能不太了解 web URL 的工作原理,以及如何使用 server.mappath()。

Web users: Web 用户:

When you have a web based url, then all html markup in a page, or even user supplied URL's are so called web based. When you have a web based url, then all html markup in a page, or even user supplied URL's are so called web based.

So, if you have a folder from the root of your web site say called MyUpLoads因此,如果您的 web 站点的根目录中有一个名为 MyUpLoads 的文件夹

Then that is just a folder in the web site path names.那么这只是 web 站点路径名称中的一个文件夹。

eg:例如:

www.mywebsite/UpLoadFiles/cat.jpg

And if you write html markup, then you can and could provide a URL to the above picute, or say with a html image control, you could set the ImageURL or "source" (src) to that file.如果您编写 html 标记,那么您可以并且可以为上面的图片提供 URL,或者使用 html 图像控制(图像控制,源代码)

And if you using IIS (and not IIS express), then of course you can add what is called a virutal folder.如果您使用 IIS(而不是 IIS express),那么您当然可以添加所谓的虚拟文件夹。 Say some big server drive on ANOHTER computer on the same network.假设同一网络上的另一台计算机上的一些大型服务器驱动器。

So, that virtual folder could be anywhere on your network, and of course AGAIN for web HTML, or web URL's, again you use this format:因此,该虚拟文件夹可以位于您网络上的任何位置,当然对于 web HTML 或 web URL,再次使用此格式:

www.mysite/MassiveFolder/info.pdf

or maybe或者可能

 localhost:5403/MyUpLoads/cat.jpg

However, in code behind?但是,在代码后面呢?

ANY code behind (c# or vb.net) ALWAYS uses plane jane WINDOWS file paths.后面的任何代码(c# 或 vb.net)总是使用平面简 WINDOWS 文件路径。 These are valid full windows file names.这些是有效的完整 windows 文件名。

That means that code behind is 100% free to open/read/use/see/play with ANY file on the computer, and any file even on the computer network.这意味着后面的代码可以 100% 自由地打开/读取/使用/查看/播放计算机上的任何文件,甚至计算机网络上的任何文件。

So when you use所以当你使用

server.mapPath("localhost:5403/MyUpLoads/cat.jpg")

Then the above is translated into a local plane jane DOS/WINDOWS file path!!!!然后上面就翻译成本地平面简DOS/WINDOWS文件路径!!!

The above may well become以上很可能变成

C:\Users\AlbertKallal\source\repos\CSharpWebApp\MyUpLoads\cat.jpg

So keep in mind:所以请记住:

web urls - HTML/asp markup in a page = web based syntax/path.

computer path: plane jane full path names like all windows software.

So, in your case?那么,在你的情况下?

 var fileName = "FilesDL".zip";
  var tempOutPutPath = @"C:/Users/SDILAP2/Desktop/ID_Esig_Files")) + fileName;

So you don't need nor want to user server.mappath, since that is ONLY for a given HTML or web based URL that you want to translate into the local computer file path system.因此,您不需要也不想使用 server.mappath,因为这仅适用于您想要转换为本地计算机路径系统的基于 HTML 或 web 的 URL 的给定计算机路径系统。

Since your path name(s) are already in that format, then no need is required.由于您的路径名称已经采用该格式,因此不需要。

in fact, keep in mind that you can use this fact to your advantage.事实上,请记住,您可以利用这一事实来发挥自己的优势。

ANY folder (or a vitural folder) will appear in your valid URL's and path names (web based).任何文件夹(或虚拟文件夹)都将出现在您的有效 URL 和路径名(基于 Web)中。

However, you might have some pdf's, or sensitive documents.但是,您可能有一些 pdf 文件或敏感文件。 So move that folder OUT of the root or web project folders.因此,将该文件夹移出根目录或 web 项目文件夹。

Now, no valid URL's exist, or are even allowed.现在,没有有效的 URL 存在,甚至是不允许的。

However, code behind?然而,背后的代码? It can run, see and use ANY file on your computer - and you use code behind to get those files - but the web site, web side of things has NO ability to use or see or get those files.它可以在您的计算机上运行、查看和使用任何文件-您使用后面的代码来获取这些文件-但是 web 站点、web 方面无法使用或查看或获取这些文件。 And you can still do things like say provide a download button, but your code behind can fetch the file, read it and pump it out to the end user (stream the file).而且您仍然可以执行诸如提供下载按钮之类的操作,但是您背后的代码可以获取文件,读取文件并将其发送给最终用户(流式传输文件)。

So you only need (have) to use the Server.MapPath function WHEN the URL comes from the web site or html markup. So you only need (have) to use the Server.MapPath function WHEN the URL comes from the web site or html markup. This will translate that web based URL into a regular good old fashion full qualified windows file path name.这会将基于 web 的 URL 转换为常规良好的老式完全合格的 windows 文件路径名。

However, if you already have that full windows path name, then no URL translate to windows file path is required.但是,如果您已经拥有完整的 windows 路径名,则不需要将 URL 转换为 windows 文件路径。

So, for the most part, your code behind can look at, see, grab and play with files on the server.因此,在大多数情况下,您的代码可以查看、查看、抓取和播放服务器上的文件。 Web users, or web based urls MUST be part of the folders in the web site, but no such restrictions exist for the code behind.基于 Web 用户或 web 的 url 必须是 web 站点中文件夹的一部分,但后面的代码不存在此类限制。

Now, when the code is deployed to a web server, often some file security rights on in place, but as a general rule, that web code behind is NOT limited nor restricted to JUST folders in the web site.现在,当代码部署到 web 服务器时,通常一些文件安全权限已到位,但作为一般规则,web 代码背后的代码不仅限于 Z2567A5EC9705EB71AC2C984033E0 站点中的 JUST 文件夹。 Those valued URL's are a restriction for the users and web browsers, and as noted, often a folder outside of the web site is used for security purposes, since no possible valid web based paths can use/see or even resolve to file outside of the root starting folder of the web site.这些有价值的 URL 是对用户和 web 浏览器的限制,并且如上所述,通常 web 站点之外的文件夹用于安全目的,因为没有可能的有效 Z2567A5EC9705EB7AC2C2C984033E0618 web 站点的根起始文件夹。

So for those existing files, you don't need server.mappath.因此,对于那些现有文件,您不需要 server.mappath。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 StreamReader Server.MapPath-给定的物理路径,预期的虚拟路径 - StreamReader Server.MapPath - Physical path given, virtual path expected Server.MapPath - 给定的物理路径,预期的虚拟路径 - Server.MapPath - Physical path given, virtual path expected c#使用Server.MapPath()找不到路径的一部分 - c# Could not find a part of the path with Server.MapPath() Server.MapPath(virtualPath)返回控制器的根目录,也不返回IIS中虚拟目录中提到的物理路径 - Server.MapPath(virtualPath) Returns root directory of controller, it is nor returns the physical path mentioned in the virtual directory in IIS Server.MapPath不会返回映射到Web-App子文件夹中的虚拟目录的正确物理路径 - Server.MapPath does not return the right physical path mapped to a virtual directory in Web-App subfolder 使用server.MapPath()时不允许使用相对虚拟路径 - Relative virtual path is not allowed when using server.MapPath() 如何使用Server.MapPath读取相对Web路径,并在文件名或C#中使用空格字符 - How to read a relative web path with Server.MapPath with space character in filename or path in C# Server.MapPath给出了错误的路径,在IIS服务器上运行时出现异常“不支持给定路径的格式”? - Server.MapPath gives wrong path, exception “The given path's format is not supported” when running on IIS server? C#类库中的Server.Mappath - Server.Mappath in C# classlibrary C:/ Users /…是物理路径,但是应该使用虚拟路径 - C:/Users/… is a physical path, but a virtual path was expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM