简体   繁体   中英

access is denied when trying to upload image to the server using asp.net web api 2

I am trying to upload image to the server with WebAPI C# Code:

 public async Task<string> Post()
    {
        try
        {
            var httpRequest = HttpContext.Current.Request;

            if (httpRequest.Files.Count > 0)
            {
                foreach(string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];

                    var filename = postedFile.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();

                    var filePath = HttpContext.Current.Server.MapPath("~/Uploads/"+ filename);

                    postedFile.SaveAs(filePath);

                    return "/uploads" + filename;
                }
            }
            else
            {
                return "file was not uploaded";
            }
        }

        catch (Exception exception)
        {
            return exception.Message;
        }
        return "hi";
    }

When I upload image through postman, I get

"Access to the path 'c:\\users\\ahmed\\source\\repos\\Election\\Election\\Uploads\\Background.png' is denied."

Check your upload folder permissions. And add everyone permission to upload folder, and test it again. Don't forget remove everyone permission when test is over.

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