public RemoteFileInfo DownloadFile(DownloadRequest request) { api.TextToLog("File request recieved: " + request.FileName); RemoteFileInfo result = new RemoteFileInfo(); try { string filePath = System.IO.Path.Combine(PATH_TO_FILES, request.FileName); System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (!fileInfo.Exists) throw new System.IO.FileNotFoundException("File not found", request.FileName); System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); result.FileName = request.FileName; result.Length = fileInfo.Length; result.FileByteStream = stream; } catch (Exception ex) { api.ErrorToLog(ex.Message); } return result; }
public void UploadFile(RemoteFileInfo request) { FileStream targetStream = null; Stream sourceStream = request.FileByteStream; string uploadFolder = PATH_TO_FILES; string filePath = Path.Combine(uploadFolder, request.FileName); using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { const int bufferLen = 65000; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0) { // save to output stream targetStream.Write(buffer, 0, count); } targetStream.Close(); sourceStream.Close(); } }