【问题标题】:Exception when sending file through Stream通过 Stream 发送文件时出现异常
【发布时间】:2014-09-04 19:02:32
【问题描述】:

我想直接从 dropbox 下载文件。我能够检索我想直接从 dropbox 下载的文件的内容。我无法通过流将文件发送到浏览器。 以下异常: mscorlib.dll 中出现“System.UnauthorizedAccessException”类型的异常,但未在用户代码中处理

附加信息:对路径“C:\Program Files (x86)\IIS Express\FirstLab_1.pdf”的访问被拒绝。 在线 使用 (FileStream fileStream = new FileStream(filename, FileMode.Create))

以下是我的代码:

public FileStreamResult Download(string bkpath)
{

    string bookname = bkpath;
    var accessToken = new OAuthToken("d2iwy26brzqhetr0", "xxxxxxxxxxxx");
    var api = new DropboxApi(ConsumerKey, ConsumerSecret, accessToken);
    var file = api.DownloadFile("dropbox", bookname);
    string path = file.Path;
    string filename = Path.GetFileName(path);
    // Create random data to write to the file. 
    byte[] dataArray = new byte[file.Data.Length];
    new Random().NextBytes(dataArray);

    using (FileStream fileStream = new FileStream(filename, FileMode.Create))
    {
        // Write the data to the file, byte by byte. 
        for (int i = 0; i < dataArray.Length; i++)
        {
            fileStream.WriteByte(dataArray[i]);
        }

        // Set the stream position to the beginning of the file.
        fileStream.Seek(0, SeekOrigin.Begin);

        // Read and verify the data. 
        for (int i = 0; i < fileStream.Length; i++)
        {
            if (dataArray[i] != fileStream.ReadByte())
            {
                Response.Write("Error writing data."); 
            }
        }
        return new FileStreamResult(fileStream, "application/pdf");
    }
}

请帮助我直接从保管箱下载文件,因为我正在解决这个异常大约很长时间并且无法获得解决方案。

【问题讨论】:

    标签: asp.net-mvc file dropbox-api access-token filestreamresult


    【解决方案1】:

    异常消息很好地描述了问题。随便一个

    a) 右击 'C:\Program Files (x86)\IIS Express\' 文件夹 -> 属性 -> 安全,然后将写入权限授予所有人。

    好多了

    b) 将文件写入临时目录。

    string filename = Path.Combine(Path.GetTempPath(), Path.GetFileName(path));
    

    【讨论】:

      猜你喜欢
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      相关资源
      最近更新 更多