Asp. NET access network mapping disk & implement File upload read function

Source: Internet
Author: User

Recently, when changing the web, encountered a problem, to cross-machine access to shared folders, to achieve normal file upload download function.

To implement this function, you can use HTTP, or the way the network map disk, today mainly to share with you the use of network mapping disk, to access shared folders across machines.

Solution:

I thought that as long as the Web server to do a disk mapping, and then the map drive as a local disk to use it, but there is always a problem, to find the information, because the IIS default account is Network_service, the account is not authorized to access the shared directory, So when we deploy the site to IIS, then accessing the mapped disk will report the "Path not found" error. So, it doesn't work to create a disk map directly, we need to create the mapping in the program with the specified account and run the IIS process with that account, with detailed steps and related code below.

Detailed steps:

(Note: A server is the same server as the ASP, and B server is the same server as the shared folder)

1, in a, b two servers create the same user name, password account, such as: Account for User1, password for pwd1;

A server User1 the user group of the account select the Default user group;

B Server User1 account needs to be removed from all user groups;

2. Create the shared folder image in Server B, and set the access account to User1;

3. New public class Wnethelper in Web project

1 usingSystem.Runtime.InteropServices;2  3  Public classWnethelper4 {5[DllImport ("Mpr.dll", EntryPoint ="WNetAddConnection2")]6     Private Static extern UINTWNetAddConnection2 (Netresource Lpnetresource,stringLppassword,stringlpUserName,UINTdwFlags);7  8[DllImport ("Mpr.dll", EntryPoint ="WNetCancelConnection2")]9     Private Static extern UINTWNetCancelConnection2 (stringLpname,UINTDwFlags,BOOLfforce);Ten   One [StructLayout (layoutkind.sequential)] A      Public classNetresource -     { -          Public intDwscope; the   -          Public intdwtype; -   -          Public intDwdisplaytype; +   -          Public intDwusage; +   A          Public stringlpLocalName; at   -          Public stringLpremotename; -   -          Public stringlpcomment; -   -          Public stringLpprovider; in     } -   to     /// <summary> +     ///make local mappings for network shares -    /// </summary> the     /// <param name= "username" >Access User name (Windows system needs to add computer name, such as: comp-1\user-1)</param> *     /// <param name= "password" >Access User Password</param> $     /// <param name= "RemoteName" >network share path (for example: \\192.168.0.9\share)</param>Panax Notoginseng     /// <param name= "LocalName" >Local map drive letter</param> -     /// <returns></returns> the      Public Static UINTWnetaddconnection (stringUsernamestringPasswordstringRemoteName,stringlocalname) +     { ANetresource Netresource =NewNetresource (); the   +Netresource.dwscope =2; -Netresource.dwtype =1; $Netresource.dwdisplaytype =3; $Netresource.dwusage =1; -Netresource.lplocalname =LocalName; -Netresource.lpremotename = Remotename.trimend ('\\'); the         UINTresult = WNetAddConnection2 (netresource, password, username,0); -  Wuyi         returnresult; the     } -   Wu      Public Static UINTWnetcancelconnection (stringNameUINTFlagsBOOLForce ) -     { About         UINTNret =WNetCancelConnection2 (name, flags, force); $   -         returnNret; -     } -}
View Code

4. Specify the running account for IIS User1

Add <identity impersonate= "true" Username= "user1" password= "Pwd1"/>; in the <system.web> of Web. config

5, before accessing the shared directory, call wnethelper.wnetaddconnection, add disk mapping

1 UINTState =0;2 if(! Directory.Exists ("Z:"))3 {4State = Wnethelper.wnetaddconnection (@"User1","PWD1",@"\\192.168.1.196\Image","Z:");5 }6 if(state. Equals (0))7 {8     //Create an upload path for a shared directory9     if(! Directory.Exists ("Z:\\upload"))Ten     { OneDirectory.CreateDirectory ("Z:\\upload")); A     } - } - Else the { -Writelog ("Add network drive Error, error number:"+State . ToString ()); -}
View Code

6. Realize upload and read function
Upload:

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

Read:

Create a new ashx file: 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);

Background:

Imgview.imageurl = "Attachment.ashx";

There is also a simpler way to access a shared file, which does not require a mapped disk

Add the following code to the Global.asax Application_Start ()

1 voidApplication_Start (Objectsender, EventArgs e)2 {3     //program code to execute at application startup4     stringstruser ="User1";5     stringStrpwd ="PWD1";6     stringStrmapurl =@"\\192.168.1.198\Image";7     8System.Diagnostics.ProcessStartInfo p =NewSystem.Diagnostics.ProcessStartInfo ("Net.exe"9," Use"+ Strmapurl +" \""+ Strpwd +"\ "/user:\""+ struser +"\"");TenP.windowstyle =System.Diagnostics.ProcessWindowStyle.Hidden; One System.Diagnostics.Process.Start (p); A}

Then in the program you can use \\192.168.1.198\Image directly to fetch.

Reference article: http://www.cnblogs.com/sqzhuyi/archive/2011/01/15/aspnet-remote.html

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

Asp. NET access network mapping disk & implement File upload read function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.