admin管理员组文章数量:1550528
业务场景:某应用部署在负载均衡的两台服务,一台是共享文件夹所在的服务器DataServer,另一台是服务器OtherServer。该应用有文件上传功能,需要上传文件到共享文件夹内。
存在问题:部署在OtherServer上的站点,不能直接读写DataServer上的文件夹(共享文件夹: \\DataServer\ShareDocument)。
解决方案:
1、在DataServer和OtherServer两台服务器上,都创建一个同名同密码的账号ShareDocUser。
注意:要保证OtherServer上的ShareDocUser账号,要有C:\Windows\Microsoft.NET 路径的读权限,因为站点代码的一些系统依赖包在此路径下(可以在OtherServer服务器把ShareDocUser用户设为管理员,就会拥有权限。如果不能设置为管理员,要保证该用户有足够的权限)
2、在DataServer的ShareDocument文件夹,添加ShareDocUser的读写权限。
3、在站点部署包的web.config内,修改system.web节点指定该站点的启动用户
<system.web>
<identity impersonate="true" userName="ShareDocUser" password="这里写密码" />
</system.web>
4、代码访问时的路径为:\\DataServer服务器ip\ShareDocument\
(如有安全限制,把web.config文件进行加密)
- 读取文件时,可以把DataServer服务器的共享文件夹挂到IIS上,通过http地址访问(随便一台服务器iis添加站点→路径输入共享文件夹路径→高级设置→物理路径凭证→输入账号密码→应用→重启站点)
-
<!-- 文件夹挂载到iis时,需要在文件夹创建web.config文件,并赋予读取权限 --> <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <directoryBrowse enabled="true" /> <security> <requestFiltering allowDoubleEscaping="true" /> </security> </system.webServer> </configuration>
- 上传文件时,可以使用HttpPostedFileBase的SaveAs方法
//文件上传 public ActionResult H5_Upload() { string IP = System.Web.Configuration.WebConfigurationManager.AppSettings["IP"]; //获取上传的文件,fileToUpload为文件的id名 HttpPostedFileBase httpPostedFile = Request.Files["fileToUpload"]; //如果接收到文件则httpPostedFile不为null,则对上传的文件进行处理,否则向客户端返回0 if (httpPostedFile == null) { //没有文件,向客户端输出0 return Content("0"); } //获取文件名 string strfileName = httpPostedFile.FileName; //获取扩展名 string strExt = Path.GetExtension(strfileName); //允许上传的文件类型 string[] strExts = { ".pdf", ".PDF" }; if (!strExts.Contains(strExt)) { return Content("该文件类型不允许上传"); } //上传目录 string dir = DateTime.Now.ToString("yyyyMMdd"); DirectoryInfo theFolder = new DirectoryInfo(@"//" + IP + ""); string strNewName = Guid.NewGuid().ToString().Substring(0, 6) + "_" + strfileName; string path = Path.Combine(theFolder.FullName, dir); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //保存文件到文件服务器 string filePath = path + @"/" + strNewName; httpPostedFile.SaveAs(filePath); return Content("上传成功,路径为:" + dir + @"/" + strNewName); }
版权声明:本文标题:IIS读写其他服务器的共享文件夹 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1727244248a1104461.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论