通过Stream发送文件时出现异常

时间:2014-09-04 19:02:33

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

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

其他信息:拒绝访问路径“C:\ Program Files(x86)\ IIS Express \ FirstLab_1.pdf”。 在线 using(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");
    }
}

请帮我从dropbox直接下载文件,因为我很长时间都在解决此异常,无法获得解决方案。

1 个答案:

答案 0 :(得分:1)

异常消息很好地描述了问题。只需

a)右键单击&#39; C:\ Program Files(x86)\ IIS Express \&#39;文件夹 - &gt;属性 - &gt;安全性并向每个人提供写入权限。

好多了

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

string filename = Path.Combine(Path.GetTempPath(), Path.GetFileName(path));
相关问题