Reputation: 31
I want to download a file directly from dropbox.I am able to retrieve the contents of a file which I want to download directly from dropbox.I am unable to send the file through stream to a browser. Following exception: An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code
Additional information: Access to the path 'C:\Program Files (x86)\IIS Express\FirstLab_1.pdf' is denied. at the line using (FileStream fileStream = new FileStream(filename, FileMode.Create))
Following is my code:
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");
}
}
Please help me out to get the file download direcly from dropbox as I am resolving this exception for a about long time and couldn't get the solution.
Upvotes: 0
Views: 1097
Reputation: 13043
The exception message describes the problem pretty well. Simply either
a) Right click on 'C:\Program Files (x86)\IIS Express\' folder -> Properties -> Security and give the write permission to everyone.
or much better
b) write the file into the temp directory.
string filename = Path.Combine(Path.GetTempPath(), Path.GetFileName(path));
Upvotes: 1