代码之家  ›  专栏  ›  技术社区  ›  Nigel Findlater

当system.io.filestream导致访问冲突错误时,为什么file.readallbytes在IIS服务器上工作?

  •  2
  • Nigel Findlater  · 技术社区  · 7 年前

    我有一个返回PDF的REST服务。当我使用文件流时,在Visual Studio中调试时会收到PDF,但当我将其部署到IIS 7时,访问被拒绝。

    [HttpGet]
    [Route("Document/Pdf/{id}")]
    [ResponseType(typeof(HttpResponseMessage))]
    public async Task<HttpResponseMessage> DocumentPdfGet(int id)
    {
            string pdfPath = System.Web.HttpContext.Current.Server.MapPath("~/test.pdf");
            System.IO.FileStream stream = new FileStream(pdfPath, FileMode.Open);
            string filename = Path.GetFileName(pdfPath);
    
            HttpResponseMessage innerResult = new HttpResponseMessage(HttpStatusCode.OK);
    
            stream.Seek(0, SeekOrigin.Begin);
            innerResult.Content = new StreamContent(stream);
            innerResult.Content.Headers.ContentLength = stream.Length;
            innerResult.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    
            ContentDispositionHeaderValue contentDisposition = null;
            if (ContentDispositionHeaderValue.TryParse("inline; filename=" + filename + ".pdf", out contentDisposition))
            {
                innerResult.Content.Headers.ContentDisposition = contentDisposition;
            }
            return innerResult;
        }
    }
    

    我得到的错误是:

    System.UnauthorizedAccessException: Access to the path 'C:\Sites\MySite\test.pdf' is denied.
       at 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, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at 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)
       at Workflow.Web.Controllers.WebApi.DocumentUIController.<DocumentPdfGet>d__11.MoveNext() in C:\agent\_work\10\s\....\Controllers\WebApi\MyControllerController.cs:line 220
    

    但当我将文件流更改为file.readallbytes时,服务器上的访问被拒绝,请参见下面的代码:

    [HttpGet]
    [Route("Document/Pdf/{id}")]
    [ResponseType(typeof(HttpResponseMessage))]
    public async Task<HttpResponseMessage> DocumentPdfGet(int id)
    {
            string userName = User.Identity.Name;
    
            Byte[] pdfFile;
            string pdfPath = System.Web.HttpContext.Current.Server.MapPath("~/test.pdf");
            string filename = Path.GetFileName(pdfPath);
            HttpResponseMessage innerResult = new HttpResponseMessage(HttpStatusCode.OK);
    
            pdfFile = File.ReadAllBytes(pdfPath);
            ByteArrayContent byteArrayContent = new ByteArrayContent(pdfFile);
    
            innerResult.Content = byteArrayContent;
            innerResult.Content.Headers.ContentLength = pdfFile.Length;
            innerResult.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    
            ContentDispositionHeaderValue contentDisposition = null;
            if (ContentDispositionHeaderValue.TryParse("inline; filename=" + filename + ".pdf", out contentDisposition))
            {
                innerResult.Content.Headers.ContentDisposition = contentDisposition;
            }
            return innerResult;
    }
    

    当system.io.filestream导致访问冲突错误时,为什么file.readallbytes在IIS服务器上工作?

    1 回复  |  直到 7 年前
        1
  •  1
  •   MacakM    7 年前

    默认情况下,文件流使用 FileAccess.ReadWrite

    FileAccess.Read

    那么如何修复代码呢?使用获取fileaccess参数并将其设置为 .