ASP.NET访问网络映射盘&实现文件上传读取功能

时间:2023-03-08 19:59:14

最近在改Web的时候,遇到一个问题,要跨机器访问共享文件夹,以实现文件正常上传下载功能。

要实现该功能,可以采用HTTP的方式,也可以使用网络映射磁盘的方式,今天主要给大家分享一下使用网络映射磁盘的方式,来跨机器访问共享文件夹。

解决方案:

   本以为只要在Web服务器做一下磁盘映射,然后把该映射盘符当做本地磁盘来使用就可以了,但是却一直有问题,查找了一下资料,是因为IIS默认账户为NETWORK_SERVICE,该账户是没权限访问共享目录的,所以当我们把站点部署到IIS上的时候,再访问映射磁盘就会报“找不到路径”的错误。所以,直接创建磁盘映射是行不通的,我们需要在程序中用指定账户创建映射,并用该账户运行IIS进程,下面来说下详细步骤及相关代码。

详细步骤:

(注:A服务器为ASP.NET程序所在服务器,B服务器为共享文件夹所在服务器)

   1、在A、B两台服务器上面创建相同用户名、密码的账户,如:账户为user1,密码为pwd1;

A服务器user1账户的用户组选择默认的user组即可;

B服务器user1账户需移出所有用户组;

2、在B服务器中创建共享文件夹Image,并设置访问账户为user1;

3、WEB项目中新建公共类WNetHelper

 using System.Runtime.InteropServices;

 public class WNetHelper
{
[DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags); [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")]
private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce); [StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public int dwScope; public int dwType; public int dwDisplayType; public int dwUsage; public string lpLocalName; public string lpRemoteName; public string lpComment; public string lpProvider;
} /// <summary>
/// 为网络共享做本地映射
/// </summary>
/// <param name="username">访问用户名(windows系统需要加计算机名,如:comp-1\user-1)</param>
/// <param name="password">访问用户密码</param>
/// <param name="remoteName">网络共享路径(如:\\192.168.0.9\share)</param>
/// <param name="localName">本地映射盘符</param>
/// <returns></returns>
public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
{
NetResource netResource = new NetResource(); netResource.dwScope = ;
netResource.dwType = ;
netResource.dwDisplayType = ;
netResource.dwUsage = ;
netResource.lpLocalName = localName;
netResource.lpRemoteName = remoteName.TrimEnd('\\');
uint result = WNetAddConnection2(netResource, password, username, ); return result;
} public static uint WNetCancelConnection(string name, uint flags, bool force)
{
uint nret = WNetCancelConnection2(name, flags, force); return nret;
}
}

4、为IIS指定运行账户user1

在Web.config的<system.web>下添加<identity impersonate="true" userName="user1" password="pwd1"/>;

5、在访问共享目录前,调用WNetHelper.WNetAddConnection,添加磁盘映射

 uint state = ;
if (!Directory.Exists("Z:"))
{
state = WNetHelper.WNetAddConnection(@"user1", "pwd1", @"\\192.168.1.196\Image", "Z:");
}
if (state.Equals())
{
//创建共享目录的上传路径
if (!Directory.Exists("Z:\\UpLoad"))
{
Directory.CreateDirectory("Z:\\UpLoad"));
}
}
else
{
WriteLog("添加网络驱动器错误,错误号:" + state.ToString());
}

6、实现上传读取功能
          上传:

file1.SaveAs(@"Z:\UpLoad\2016-01-26_124937.png");

读取:

     新建一个ashx文件:Attachment.ashx

byte[] datas = System.IO.File.ReadAllBytes("\\192.168.1.198\Image\UpLoad\2016-01-26_124937.png");       
          context.Response.OutputStream.Write(datas, 0, datas.Length);

后台:

imgView.ImageUrl = "Attachment.ashx";

还有一种比较简单的方式来访问共享文件,它不需要映射磁盘

在Global.asax的Application_Start()中添加下面的代码即可

 void Application_Start(object sender, EventArgs e)
{
// 应用程序启动时执行的程序代码
string strUser = "user1";
string strPwd = "pwd1";
string strMapUrl = @"\\192.168.1.198\Image"; System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo("net.exe"
, "use " + strMapUrl + " \"" + strPwd + "\" /user:\"" + strUser + "\"");
p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process.Start(p);
}

然后在程式里面就可以直接用\\192.168.1.198\Image来取.

参考文章:http://www.cnblogs.com/sqzhuyi/archive/2011/01/15/aspnet-remote.html

http://www.cnblogs.com/sunyanjun/articles/2419399.html