System.UnauthorizedAccessException的一个简单的ASP.Net File.IO操作 [英] System.UnauthorizedAccessException on a simple ASP.Net File.IO operations

查看:118
本文介绍了System.UnauthorizedAccessException的一个简单的ASP.Net File.IO操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET(托管)我需要一个简单的文件IO操作读出一个文件的数据,然后强制下载。它真的那么简单。

In ASP.NET (hosted) I need a simple file IO operation to read data out of a file and then force a download. Its really that simple.

我不断收到一个执行以下code当 System.UnauthorizedAccessException的

I keep getting a System.UnauthorizedAccessException when executing the following code:

System.IO.FileStream fs = 
      new System.IO.FileStream(path, System.IO.FileMode.Open);

本地它工作正常,但是当我上传到共享的托管帐户,我得到上述的异常。

Locally it works fine, but when i upload to the shared hosting account i get the above exception.

请告诉我奇怪的是,在浏览器中,如果我输入完整的文件路径,我可以看到和使用的文件。

Whats strange is that in the browser if i enter the full path to the file, i can see and use the file.

推荐答案

由于每个人都提到,这很可能是因为ASP进程没有拿到安全权限来访问文件目录。

As everyone else has mentioned, this is most likely because the ASP process hasn't got security permissions to access the file directory.

如果您可以通过浏览器访问该文件也许你可以使用的HttpWebRequest,而不是File.Open读取该文件。

If you can access the file via your browser perhaps you could read the file by using a HttpWebRequest rather than File.Open.

这将使意义,如果你没有对服务器的管理控制。

This would make sense if you don't have admin control over the server.

下面是一些示例code使用的HttpWebRequest柜面它会帮助:

Here's some sample code for using HttpWebRequest incase it'd help:

    /// <summary>
    /// Submits a request to the specified url and returns the text response, or string.Empty if the request failed.
    /// </summary>
    /// <param name="uri">The url to submit the request to.</param>
    /// <returns>The text response from the specified url, or string.Empty if the request failed.</returns>
    protected string getPageResponse(string url)
    {
        //set the default result
        string responseText = string.Empty;

        //create a request targetting the url
        System.Net.WebRequest request = System.Net.HttpWebRequest.Create(url);
        //set the credentials of the request
        request.Credentials = System.Net.CredentialCache.DefaultCredentials;

        //get the response from the request
        System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
        //get the response code (format it as a decimal)
        string statusCode = response.StatusCode.ToString("d");

        //only continue if we had a succesful response (2XX or 3XX)
        if (statusCode.StartsWith("2") || statusCode.StartsWith("3"))
        {
            //get the response stream so we can read it
            Stream responseStream = response.GetResponseStream();
            //create a stream reader to read the response
            StreamReader responseReader = new StreamReader(responseStream);
            //read the response text (this should be javascript)
            responseText = responseReader.ReadToEnd();
        }

        //return the response text of the request
        return responseText;
    }

这篇关于System.UnauthorizedAccessException的一个简单的ASP.Net File.IO操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆