且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何让客户端选择文件目录和名称以将文件保存在ASP.NET webform中?

更新时间:2022-04-20 22:07:06

你没有。您没有任何访问客户端计算机的权限,也无法直接在其上存储任何内容 - 您必须将文件下载到用户,然后他们决定如何处理它。 C#代码总是在服务器上运行,永远不在客户端上运行,并且只能访问服务器资源。



并试图让用户决定你的路径服务器是愚蠢的 - 而且很危险 - 他们(实际上你)不知道生产机器中允许存储文件的位置,除了相对于网站的根目录。而且你不知道那是什么,因为它没有在文件系统中修复 - 这就是为什么你有Server.MapPath将相对路径转换为文件I / O函数的正确路径。



这似乎可以在开发中使用,但这是因为客户端和服务器在同一台计算机上运行。在生产中,两者可以分开数千英里,而服务器根本无法访问客户端文件系统。
You don't. You don't have any access to the client machine, and can't store anything on there directly - you have to download the file to the user and they then decides what to do with it. C# code always runs on the Server, never on the Client, and can only ever access Server resources.

And trying to get the user to decide on a path on your server is silly - and dangerous - they (and indeed you) have no idea where in the production machine you are permitted to store files, except relative to the root directory of your website. And you don;t know what that is because it's not fixed in the file system - that's why you have Server.MapPath to convert a relative path to a "proper" path for File I/O functions.

This may appear to work in development but that's because the Client and Server are running on the same computer. In production the two can be separated by thousands of miles, and the Server has no access at all to the client file system.