I created a service project and trying to upload file to drop box. Code for small file work fine.
For large file I used chunk but its not working neither giving any error as well
public partial class Service1 : ServiceBase
{
Timer _timer = new Timer();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
FtpFileUpload.WriteToFile("Simple Service started {0} ");
//_timer = new Timer(10 * 60 * 1000); // every 10 minutes
_timer = new Timer(3000000); // every 10 minutes
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Start(); // <- important
}
catch (Exception ex)
{
FtpFileUpload.WriteToFile("Service Error Exception OnStart: {0} " + ex.Message);
FtpFileUpload.WriteToFile("Service Error Exception OnStart" + ex.StackTrace);
}
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
FtpFileUpload.WriteToFile("ScheduleBackupService: Method Call");
FileUploadToDropbox(FilePath, name, FilePath);
}
private static void CreateFolder(string fileName, out string backupPath, out string path)
{
backupPath = Path.Combine(fileName, "BackUpFiles");
if (!Directory.Exists(backupPath))
{
FtpFileUpload.CreateDirectory(backupPath);
}
FtpFileUpload.WriteToFile("Backup Path (" + backupPath + ")");
FtpFileUpload.WriteToFile(" System Path: {0} " + fileName);
path = (fileName + @"\JsonFolder\Details.json");
FtpFileUpload.WriteToFile("Json Full path" + path);
}
private async Task FileUploadToDropbox(string filePath, string fileName, string fileContent)
{
const int ChunkSize = 4096 * 1024;
try
{
var dbx = new DropboxClient("");
using (var stream = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(fileContent)))
{
ChunkUpload(dbx, fileName, fileContent);
FtpFileUpload.WriteToFile("FileUpload Succcessfully Dropbox: {0}" + (fileName));
}
}
catch (Exception ex)
{
FtpFileUpload.WriteToFile("Service Error FileUpload Dropbox: {0} " + ex.Message);
FtpFileUpload.WriteToFile("Service Error Exception ScheduleBackupService" + ex.StackTrace);
}
}
private void ChunkUpload(DropboxClient client, string fileName, string Content)
{
try
{
Console.WriteLine("Chunk upload file...");
const int chunkSize = 1024 * 1024;
FileStream fs = new FileStream(Content, FileMode.Open);
byte[] data = new byte[fs.Length];
var fileContent = data;
new Random().NextBytes(fileContent);
using (var stream = new MemoryStream(fileContent))
{
int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);
byte[] buffer = new byte[chunkSize];
for (var idx = 0; idx < numChunks; idx++)
{
Console.WriteLine("Start uploading chunk {0}", idx);
var byteRead = stream.Read(buffer, 0, chunkSize);
using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
{
if (idx == 0)
{
//var result = await client.Files.UploadSessionStartAsync(body: memStream);
var result = Task.Run(async () => await client.Files.UploadSessionStartAsync(body: memStream)).Result;
sessionId = result.SessionId;
}
else
{
UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));
if (idx == numChunks - 1)
{
Task.Run(async () => await client.Files.UploadSessionFinishAsync(cursor, new CommitInfo("/BackFile/" + fileName), memStream));
sessionId = null;
}
else
{
Task.Run(async () => await client.Files.UploadSessionAppendV2Async(cursor, body: memStream));
}
}
}
}
}
}
catch (Exception ex)
{
string a = ex.Message;
throw;
}
}
protected override void OnStop()
{
FtpFileUpload.WriteToFile("Simple Service stopped {0} " + DateTime.Now);
//this.Schedular.Dispose();
}
internal class DbDetailsList
{
public string ServerType { get; set; }
public string Authentication { get; set; }
public string DbLogin { get; set; }
public string DbPassword { get; set; }
public string DbServerName { get; set; }
public string DbString { get; set; }
public string DbBackList { get; set; }
public string FTPName { get; set; }
public string FtpUserName { get; set; }
public string FtpPassword { get; set; }
}