我正在尝试将文件保存到磁盘,但出现 UnauthorizedAccessException.错误提示我必须获得对该文件夹的正确权限,并且我已经尝试了我能找到的所有可能的用户,但它不起作用.
I'm trying to save a file to the disk but I get UnauthorizedAccessException. The error says I have to get the right permissions on the folder, and I've tried every possible user I can find but it doesn't work.
尝试了以下用户
- 网络
- 网络服务
- IUSR
- IUSR_[计算机名]
并在没有它的情况下获得全部权利.
And given the full rights without it working.
我发现真正奇怪的是,我在尝试保存文件之前创建了一个目录,并且效果很好,当我尝试将文件保存到该新目录时,我收到了 UnautorhizedAccessException.
What I find really strange is that I create a directory before I try to save the file and that works perfectly, it's when trying to save a file to that new directory that I get the UnautorhizedAccessException.
代码如下:
[HttpPost] public ActionResult Images(HttpPostedFileBase file, string boatId) { if (file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Content/Images/" + boatId)); Directory.CreateDirectory(path); file.SaveAs(path); } return View($"Filen på {boatId} har laddats upp"); }对我缺少的东西有什么想法吗?
Any ideas at what I'm missing?
推荐答案结果我试图做的是保存文件夹而不是文件,我忘记将文件名与路径结合起来.
Turns out what I was trying to do was saving the folder and not the file, I forgot to combine the fileName with the path.
将保存部分更改为以下内容:
Changed the Save part to the following:
file.SaveAs(Path.Combine(path, fileName));这为我解决了整个问题.
Which solved the whole thing for me.
更多推荐
保存文件时出现 UnauthorizedAccessException,但可以创建目录
发布评论