简体   繁体   English

C#访问另一台计算机上的路径被拒绝

[英]C# Access to path on another computer denied

I am currently working on Web Api 2 project.我目前正在开发 Web Api 2 项目。 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.我的方法是,它使用多部分表单数据在 API 中接收图像作为输入,并将其上传到服务器上。 The directory I am using is a virtual directory我使用的目录是一个虚拟目录

"/Images" = "\\192.168.12.28\Share Folder\PIJ Share" "/Images" = "\\192.168.12.28\共享文件夹\PIJ 共享"

"/tes2" = "C:\Dummy" "/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以管理员身份打开 Visual Studio 2013

  2. Adding permission on the specified folder (NETWORK SERVICE, IIS_IUSR, pij.api (the name of my application pool), DefaultAppPool, Everyone).添加指定文件夹的权限(NETWORK SERVICE、IIS_IUSR、pij.api(我的应用程序池的名称)、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.我还尝试使用文件资源管理器将图像文件添加到“\192.168.12.28\Share Folder\PIJ Share”。 And it works.它有效。 What should I do ?我应该怎么办 ?

Thank you very much.非常感谢。

Update (1.1)更新 (1.1)

I tried to execute the code without IIS (using localhost on visual studio 2013) and the file uploaded successfully.我尝试在没有 IIS 的情况下执行代码(在 Visual Studio 2013 上使用 localhost)并且文件成功上传。 But when I execute it with IIS ( http://api.pijmobile.local/ ), I get the Access to the path denied error.但是当我使用 IIS ( http://api.pijmobile.local/ ) 执行它时,我得到了 Access to the path denied 错误。

It seems that the problem lies in the IIS settings.看来问题出在 IIS 设置上。 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打开 Internet 信息服务 (IIS) 管理器,展开您的计算机名称,单击应用程序池

  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插入您之前创建的用户和密码 > OK > OK > OK

在此处输入图像描述

  1. Thats all, you will be able to access the network share and view it.就是这样,您将能够访问网络共享并查看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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