首页 文章

读取文件时出现UnauthorizedAccessException

提问于
浏览
0

我有和ASP Web API,我试图从那里的电话返回一个文件 .

在我的服务器上,我一直收到错误:

System.UnauthorizedAccessException:拒绝访问路径“E:\ Data \ Docs \ specific \ document.pdf” . 在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)at System.IO.FileStream.Init(String path,FileMode mode,FileAccess access,Int32 rights,Boolean useRights,FileShare share,Int32 bufferSize,FileOptions options,SECURITY_ATTRIBUTES secAttrs) ,System msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost)在System.IO.FileStream..ctor(String path,FileMode mode,FileAccess access,FileShare share,Int32 bufferSize,FileOptions options,String msgPath,Boolean bFromProxy) . GIS.Backend.Assets.WebApi.Controllers.DocumentController.Get(String url)中GIS.Backend.Assets.BLL.Document.GetByUrl(String url)的IO.FileStream..ctor(String path,FileMode mode)

我猜这是出错的地方:

string documentenPath = System.Web.Configuration.WebConfigurationManager.AppSettings["DocumentenDir"].ToString();
        string fullUrl = documentenPath + url;

        Stream file = new FileStream(fullUrl, FileMode.Open);
        return file;

我已将IIS_IUSRS设置为对“Docs”文件夹具有读取权限,因此它应该能够正确读取吗?

1 回答

  • 2

    使用FileAccess.Read标志打开Stream

    FileStream fs = new FileStream(fullUrl, FileMode.Open, FileAccess.Read);
    

相关问题