Reputation: 419
I am trying to download a list of files, but isn't really sure of how to proceed. As the topic says, I am using DropNet, and this is the procedure I am trying to download the files with:
Get a list of all files in my applications dedicated folder and store them in a List as strings.
Then trying the following:
foreach (string file in files)
{
_client.GetFileAsync("/" +file,
(response) =>
{
using(FileStream fs = new FileStream(path +file +".gttmp", FileMode.Create))
{
for(int i = 0; i < response.RawBytes.Length; i++)
{
fs.WriteByte(response.RawBytes[i]);
}
fs.Seek(0, SeekOrigin.Begin);
for(int i = 0; i < response.RawBytes.Length; i++)
{
if(response.RawBytes[i] != fs.ReadByte())
{
MessageBox.Show("Error writing data for " +file);
return;
}
}
}
},
(error) =>
{
MessageBox.Show("Could not download file " +file, "Error!");
});
}
Unfortunately it doesn't seem to work at all. Anyone using DropNet and could suggest me something that would work?
Upvotes: 1
Views: 2385
Reputation: 23
Your code to download file asynchronously works fine, I tried in following way and it proceeds without error.
client.GetFileAsync("/novemberrain.mp3",
(response) =>
{
using (FileStream fs = new FileStream(@"D:\novemberrain.mp3", FileMode.Create))
{
for (int i = 0; i < response.RawBytes.Length; i++)
{
fs.WriteByte(response.RawBytes[i]);
}
}
MessageBox.Show("file downloaded");
},
(error) =>
{
MessageBox.Show("error downloading");
});
Upvotes: 1
Reputation: 419
Used synronous method instead:
foreach (string file in files)
{
var fileBytes = _client.GetFile("/" + file);
using (FileStream fs = new FileStream(path +file + ".gttmp", FileMode.Create))
{
for (int i = 0; i < fileBytes.Length; i++)
{
fs.WriteByte(fileBytes[i]);
}
fs.Seek(0, SeekOrigin.Begin);
for (int i = 0; i < fileBytes.Length; i++)
{
if (fileBytes[i] != fs.ReadByte())
{
MessageBox.Show("Error writing data for " + file);
break;
}
}
}
}
Upvotes: 1