简体   繁体   中英

C# Access to path on another computer denied

I am currently working on Web Api 2 project. The method I am working at is an upload image method.

Here is my code :

    public IHttpActionResult Upload()
    {
        string UserID = "12345";

        // LOCAL VARIABLE
        dynamic ExpObj = new ExpandoObject();

    // Virtual Directory Located on My Computer
        var FilePath = HttpContext.Current.Server.MapPath("/Images");

    // Virtual Directory Located on Another Computer (Located on LAN Network)
        var FilePath2 = HttpContext.Current.Server.MapPath("/tes2");

        if (HttpRequest.Files.Count > 0)
        {
            foreach (string file in HttpRequest.Files)
            {
                // GET UPLOADED IMAGE
                var PostedFile = HttpRequest.Files[file];

                // SET FILE NAME ( USERID + Right(FileName,10) )
                string FileName = GetFileName(UserID, PostedFile.FileName, 10);

                // SAVE IMAGE
                //PostedFile.SaveAs(FilePath + FileName);
                string ImagePath = Path.Combine(FilePath, FileName);
                string ImagePath2 = Path.Combine(FilePath2, FileName);

                PostedFile.SaveAs(ImagePath2);
                File.Copy(ImagePath2, ImagePath);

                ExpObj.imageURL = ServerUrl + ServerPath + FileName;
            }
        }
        else
        {
            ErrCode = -900;
        }

        // RETURN IF GOT ERROR
        if (ErrCode < 0)
        {
            return StatusCode((HttpStatusCode)(ErrCode * (-1)));
        }

        // RETURN
        return Ok(ExpObj);
    }

What my method does is, it receives images as input in API using multipart form data and uploads it on server. The directory I am using is a virtual directory

"/Images" = "\\192.168.12.28\Share Folder\PIJ Share"

"/tes2" = "C:\Dummy"

When I am trying to upload it to my own computer it works, but when I'm trying to upload it on another computer it got this error.

Access to the path denied error

Here's what I have tried to fix it :

  1. Opening Visual Studio 2013 as administrator

  2. Adding permission on the specified folder (NETWORK SERVICE, IIS_IUSR, pij.api (the name of my application pool), DefaultAppPool, Everyone). I have given every user Read/Write permission.

None of these worked. I also tried to add an image file to "\192.168.12.28\Share Folder\PIJ Share" using File Exploler. And it works. What should I do ?

Thank you very much.

Update (1.1)

I tried to execute the code without IIS (using localhost on visual studio 2013) and the file uploaded successfully. But when I execute it with IIS ( http://api.pijmobile.local/ ), I get the Access to the path denied error.

It seems that the problem lies in the IIS settings. Still searching for answers.

After days of researching finally I found the answer. After reading this link . These are the steps that I done :

  1. Creating a user in my computer and the remote server (Control panel > Administrator Tools > Computer Management > In Computer management Expand the Tree " Local users and Groups" > Right Click on Users > New Users)

  2. Fill the username and password

    在此处输入图像描述

  3. Open Internet Information Services (IIS) Manager, expand your computer name, click application pools

  4. Right click on your website application pool > Advanced Settings
  5. In Advanced Settings Window, click the ... beside identity
  6. Choose custom account > click set
  7. Insert the user and password that you made earlier > OK > OK > OK

在此处输入图像描述

  1. Thats all, you will be able to access the network share and view it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM