Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 当System.IO.FileStream导致访问冲突错误时,为什么File.ReadAllBytes在IIS服务器上工作_Asp.net_Asp.net Web Api - Fatal编程技术网

Asp.net 当System.IO.FileStream导致访问冲突错误时,为什么File.ReadAllBytes在IIS服务器上工作

Asp.net 当System.IO.FileStream导致访问冲突错误时,为什么File.ReadAllBytes在IIS服务器上工作,asp.net,asp.net-web-api,Asp.net,Asp.net Web Api,我有一个返回pdf的REST服务。当我使用文件流时,我在VisualStudio中调试时收到pdf,但当我将其部署到IIS 7时,访问被拒绝 [HttpGet] [Route("Document/Pdf/{id}")] [ResponseType(typeof(HttpResponseMessage))] public async Task<HttpResponseMessage> DocumentPdfGet(int id) { string pdfPath = S

我有一个返回pdf的REST服务。当我使用文件流时,我在VisualStudio中调试时收到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;
    }
}
[HttpGet]
[路线(“Document/Pdf/{id}”)]
[响应类型(类型(HttpResponseMessage))]
公共异步任务文档PDFGET(int id)
{
字符串pdfPath=System.Web.HttpContext.Current.Server.MapPath(“~/test.pdf”);
System.IO.FileStream stream=新文件流(pdfPath,FileMode.Open);
字符串文件名=Path.GetFileName(pdfPath);
HttpResponseMessageInnerResult=新的HttpResponseMessage(HttpStatusCode.OK);
stream.Seek(0,SeekOrigin.Begin);
innerResult.Content=新的流内容(流);
innerResult.Content.Headers.ContentLength=流.Length;
innerResult.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/pdf”);
ContentDispositionHeaderValue contentDisposition=null;
if(ContentDispositionHeaderValue.TryParse(“inline;filename=“+filename+”.pdf”,out contentDisposition))
{
innerResult.Content.Headers.ContentDisposition=ContentDisposition;
}
返回结果;
}
}
我得到的错误是:

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
System.UnauthorizedAccessException:对路径“C:\Sites\MySite\test.pdf”的访问被拒绝。
在System.IO.\uuu Error.WinIOError(Int32 errorCode,字符串maybeFullPath)
在System.IO.FileStream.Init(字符串路径、文件模式、文件访问权限、Int32权限、布尔用户权限、文件共享、Int32缓冲大小、文件选项选项、安全属性secAttrs、字符串msgPath、布尔bFromProxy、布尔useLongPath、布尔checkHost)
位于System.IO.FileStream..ctor(字符串路径、文件模式、文件访问权限、文件共享、Int32 bufferSize、文件选项选项、字符串msgPath、布尔bFromProxy)
位于System.IO.FileStream..ctor(字符串路径,文件模式)
在C:\agent\\u work\10\s\..\Controllers\WebApi\MyControllerController.cs中的Workflow.Web.Controllers.WebApi.DocumentUIController.d\u 11.MoveNext()处
但是,当我将FileStream更改为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;
}
[HttpGet]
[路线(“Document/Pdf/{id}”)]
[响应类型(类型(HttpResponseMessage))]
公共异步任务文档PDFGET(int id)
{
字符串userName=User.Identity.Name;
字节[]Pdfile;
字符串pdfPath=System.Web.HttpContext.Current.Server.MapPath(“~/test.pdf”);
字符串文件名=Path.GetFileName(pdfPath);
HttpResponseMessageInnerResult=新的HttpResponseMessage(HttpStatusCode.OK);
pdfFile=File.ReadAllBytes(pdfPath);
ByteArrayContent ByteArrayContent=新的ByteArrayContent(pdfFile);
innerResult.Content=byteArrayContent;
innerResult.Content.Headers.ContentLength=pdfFile.Length;
innerResult.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/pdf”);
ContentDispositionHeaderValue contentDisposition=null;
if(ContentDispositionHeaderValue.TryParse(“inline;filename=“+filename+”.pdf”,out contentDisposition))
{
innerResult.Content.Headers.ContentDisposition=ContentDisposition;
}
返回结果;
}

当System.IO.FileStream导致访问冲突错误时,File.ReadAllBytes为什么在IIS服务器上工作?

默认情况下,FileStream使用
FileAccess.ReadWrite
,因此当应用程序没有写入权限(可能是这种情况)时,它会抛出错误

另一方面,File.ReadAllBytes只使用
FileAccess.Read
,因此应用程序只需要具有读取权限


那么如何修复代码呢?使用接受FileAccess参数的FileStream构造函数,并将其设置为
FileAccess.Read

这完全正常工作的System.IO.FileStream stream=new FileStream(pdfPath,FileMode.Open,FileAccess.Read);