public HttpResponseMessage GetDownload() { string Path = SelDate.SelectDownloadFile; if (SelDate.SelectDownloadFile.Length == 0) { return(Request.CreateResponse(HttpStatusCode.NotFound, "Не отправлен запрос на имя скачиваемого файла!")); } if (WorkingBD.IsExistFile(null, Path)) { try { string FileName = System.IO.Path.GetFileName(Path); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(Path, FileMode.Open, FileAccess.Read); result.Content = new StreamContent(stream); result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = FileName }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return(result); } catch (Exception x) { return(Request.CreateResponse(HttpStatusCode.NotFound, x.Message)); } } else { return(null); } }
public ServiceApi() { try { //Test: http://localhost:8080/api/Convert/c:\files\file.bin var config = new HttpSelfHostConfiguration(baseAddress); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); Console.WriteLine("Instantiating server..."); WorkingBD.SaveLog("Инициализация сервера..."); server = new HttpSelfHostServer(config); } catch (Exception x) { Console.WriteLine("Error ServiceApi: " + x.Message); WorkingBD.SaveLog("Error ServiceApi: " + x.Message); Console.ReadLine(); } }
public void Stop() { server.CloseAsync(); server.Dispose(); Console.WriteLine("Server stop!" + baseAddress); WorkingBD.SaveLog("Сервер остановлен"); }
public string DeleteDataBDSQL(string id) { string NameFile = id; string result = ""; string pathFD = WorkingBD.GetPathFileName(null, NameFile); if (pathFD.Length != 0) { if (WorkBinFiles.DeleteFile(pathFD) == true) { result = "Файл удален : " + NameFile + " [" + DateTime.Now + "]"; } else { result = "Файл НЕ удален : " + NameFile + " [" + DateTime.Now + "]"; } } else { result = "Файл для удаления не найден : " + NameFile + " [" + DateTime.Now + "]"; } WorkingBD.SaveLog(result); return(result); }
public TradeRecord GetSelectDataBDSQL(int id) { TradeRecord result = WorkingBD.SelectDataBDSQL(id); Console.WriteLine("Get [TradeRecord] from id: " + id); WorkingBD.SaveLog("Отправка одной записи по ID: " + id); return(result); }
private static void StartServer() { try { serv = new ServiceApi(); serv.Start(); Console.ReadLine(); } catch (Exception x) { Console.WriteLine("Error StartServer: " + x.Message); WorkingBD.SaveLog("Error StartServer: " + x.Message); Console.ReadLine(); } }
static void Main(string[] args) { try { StartServer(); if (serv != null) { serv.Stop(); } } catch (Exception x) { Console.WriteLine("Error main: " + x.Message); WorkingBD.SaveLog("Error main: " + x.Message); Console.ReadLine(); } }
public static HeaderTradeRecord ReadBinFile(string path = @"C:\Files\file.dat") { HeaderTradeRecord fileBinRead = new HeaderTradeRecord(); fileBinRead.trades = new List <TradeRecord>(); try { CreateBinFile(path); if (File.Exists(path)) { using (BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open))) { while (reader.PeekChar() > -1) { if (fileBinRead.trades.Count == 0) { fileBinRead.version = reader.ReadInt32(); fileBinRead.type = reader.ReadString(); } int id = reader.ReadInt32(); int account = reader.ReadInt32(); double volume = reader.ReadDouble(); string comment = reader.ReadString(); fileBinRead.trades.Add(new TradeRecord { id = id, account = account, comment = comment, volume = volume }); } } Console.WriteLine("fileBinRead: " + fileBinRead); } } catch (Exception e) { Console.WriteLine("Error ReadBinFile: " + e.Message); WorkingBD.SaveLog("Error ReadBinFile: " + e.Message); } return(fileBinRead); }
public static void CreateBinFile(string path = @"C:\Files\file.dat") { try { if (!File.Exists(path)) { //string dir = Path.GetDirectoryName(path); //if (Directory.Exists(dir) == false) //{ // try // { // Directory.CreateDirectory(dir); // } // catch (Exception xd) // { // Console.WriteLine("Error CreateBinFile.CreateDirectory: " + xd.Message); // } //} CreateBinFileStruct objCreate = new CreateBinFileStruct(); HeaderTradeRecord fileBin = objCreate.GetBinFile(); using (BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.OpenOrCreate))) { writer.Write(fileBin.version); writer.Write(fileBin.type); foreach (TradeRecord t in fileBin.trades) { writer.Write(t.id); writer.Write(t.account); writer.Write(t.volume); writer.Write(t.comment); } } } } catch (Exception e) { Console.WriteLine("Error CreateBinFile: " + e.Message); WorkingBD.SaveLog("Error CreateBinFile: " + e.Message); } }
private static Task<bool> ConvertFileCSV_Task(string PathFile = "") { return Task.Run(() => { try { HeaderTradeRecord fileBinRead = WorkBinFiles.ReadBinFile(PathFile); if (fileBinRead.trades.Count>0) { WorkingBD.InsertDataCSV(PathFile, fileBinRead); } return true; } catch (Exception x) { Console.WriteLine("Error ConvertFileCSV_Task: " + x.Message); } return false; }); }
public static bool DeleteFile(string Path) { try { //Response.ContentType = ContentType; //Response.AppendHeader("Content-Disposition", "attachment; filename=myFile.txt"); //Response.WriteFile(Server.MapPath("~/uploads/myFile.txt")); //Response.Flush(); File.Delete(Path); //Response.End(); WorkingBD.RemoveFileList(null, Path); return(true); } catch (Exception e) { Console.WriteLine("Error DeleteFile: " + e.Message); WorkingBD.SaveLog("Error DeleteFile: " + e.Message); } return(false); }
public string PostConvert([FromBody] ConvFile data) { string Path = ""; string TypeF = "SQL"; if (data != null) { Path = data.Path; TypeF = data.TypeF; } if (TypeF == "SQL") { ConvertingFile.ConvertFileSQL(Path); } else { ConvertingFile.ConvertFileCSV(Path); } string result = "[" + DateTime.Now + "] Файл сконвертирован в тип " + TypeF + " Ссылка для скачивания файла " + Path + " -> " + "http://localhost:8080/api/Download/"; WorkingBD.SaveLog("Файл сконвертирован " + Path + " в тип " + TypeF); return(result); }
public void Start() { server.OpenAsync(); Console.WriteLine("Server run..." + baseAddress); WorkingBD.SaveLog("Сервер запущен"); }