Fir*_*ter 2 c# asp.net io file asp.net-web-api
我有和 ASP Web API,我试图从那里的调用返回一个文件。
在我的服务器上,我不断收到错误消息:
System.UnauthorizedAccessException:拒绝访问路径“E:\Data\Docs\specific\document.pdf”。在 System.IO.__Error.WinIOError(Int32 errorCode, String MaybeFullPath)
在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs , String 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) at System.IO.FileStream..ctor(String path, FileMode mode)在 GIS.Backend.Assets.BLL.Document.GetByUrl(String url) 在 GIS.Backend.Assets.WebApi.Controllers.DocumentController.Get(String url)
我猜这是出错的地方:
string documentenPath = System.Web.Configuration.WebConfigurationManager.AppSettings["DocumentenDir"].ToString();
string fullUrl = documentenPath + url;
Stream file = new FileStream(fullUrl, FileMode.Open);
return file;
Run Code Online (Sandbox Code Playgroud)
我已将 IIS_IUSRS 设置为对“Docs”文件夹具有读取权限,因此它应该能够正确读取吗?
小智 6
使用 FileAccess.Read 标志打开流
FileStream fs = new FileStream(fullUrl, FileMode.Open, FileAccess.Read);
Run Code Online (Sandbox Code Playgroud)