public static List <FileTaskDTO> getTaskFiles(string id_task) { List <FileTaskDTO> fileDTOList = new List <FileTaskDTO>(); using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["connectionRRHHDatabase"].ConnectionString)) { SqlCommand command = new SqlCommand("usp_get_taskFiles", connection); command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.Add("@id_task", SqlDbType.NVarChar); command.Parameters["@id_task"].Value = id_task; command.Connection.Open(); SqlDataReader rdr = command.ExecuteReader(); while (rdr.Read()) { FileTaskDTO fileDTO = new FileTaskDTO(); fileDTO.name = rdr["name"].ToString(); fileDTO.description = rdr["description"].ToString(); fileDTO.createdDate = rdr["createdDate"].ToString(); fileDTO.id_taskFile = rdr["id_taskFile"].ToString(); fileDTO.fileType = rdr["fileType"].ToString(); fileDTO.fileName = rdr["fileName"].ToString(); fileDTO.task_id = rdr["task_id"].ToString(); byte[] file = (byte[])rdr["fileData"]; fileDTO.fileBase64 = Convert.ToBase64String(file); fileDTOList.Add(fileDTO); } }; return(fileDTOList); }
public IHttpActionResult postTaskFile(FileTaskDTO pTaskFile) { if (!TaskFilesData.postFile(pTaskFile)) { return(BadRequest()); } return(Ok()); }
public async Task <bool> postTaskFile(FileTaskDTO taskFileDTO) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(_BaseAddress); var serializer = new JavaScriptSerializer(); //taskFileDTO.fileBase64 = Convert.ToBase64String(taskFileDTO.fileData); //taskFileDTO.fileData = null; serializer.MaxJsonLength = Int32.MaxValue; var userJson = serializer.Serialize(taskFileDTO); HttpContent contentPost = new StringContent(userJson, Encoding.UTF8, "application/json"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", getToken()); HttpResponseMessage response = client.PostAsync("api/tasks/files", contentPost).Result; return(response.IsSuccessStatusCode); } }
public ActionResult UploadTaskFile(AddTaskFileModel model) { if (Request.Files.Count > 0) { try { HttpFileCollectionBase files = Request.Files; for (int i = 0; i < files.Count; i++) { HttpPostedFileBase file = files[i]; string fname = file.FileName; string fileName = Path.GetFileName(fname); string fileExtension = Path.GetExtension(fileName).ToLower(); int fileSize = file.ContentLength; Stream stream = file.InputStream; BinaryReader binaryReader = new BinaryReader(stream); byte[] bytes = binaryReader.ReadBytes((int)stream.Length); FileTaskDTO fileDTO = new FileTaskDTO(); fileDTO.fileData = bytes; fileDTO.name = model.name; fileDTO.description = model.description; fileDTO.fileName = fileName; fileDTO.fileType = file.ContentType; fileDTO.task_id = model.id_task; fileDTO.userLog = Request.Cookies["user_id"].Value; if (taskProvider.postTaskFile(fileDTO).Result) { return(_TaskFilesList(fileDTO.task_id)); } } return(new HttpStatusCodeResult(404, "Error, el archivo no se puede cargar")); } catch (Exception ex) { return(new HttpStatusCodeResult(404, "Error, el archivo no se puede cargar")); } } else { return(new HttpStatusCodeResult(404, "Error, No se selecciono ningun archivo")); } }
//-------------------------------------------------- Creates ----------------------------------------------------------------- public static bool postFile(FileTaskDTO pFileDTO) { using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["connectionRRHHDatabase"].ConnectionString)) { SqlCommand command = new SqlCommand("usp_insert_taskFile", connection); command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.Add("@id_task", SqlDbType.BigInt); command.Parameters["@id_task"].Value = pFileDTO.task_id; command.Parameters.Add("@name", SqlDbType.NVarChar); command.Parameters["@name"].Value = pFileDTO.name; command.Parameters.Add("@description", SqlDbType.NVarChar); command.Parameters["@description"].Value = pFileDTO.description; command.Parameters.Add("@fileData", SqlDbType.VarBinary); command.Parameters["@fileData"].Value = pFileDTO.fileData; command.Parameters.Add("@fileType", SqlDbType.NVarChar); command.Parameters["@fileType"].Value = pFileDTO.fileType; command.Parameters.Add("@fileName", SqlDbType.NVarChar); command.Parameters["@fileName"].Value = pFileDTO.fileName; command.Parameters.Add("@userLog", SqlDbType.Int); command.Parameters["@userLog"].Value = pFileDTO.userLog; command.Connection.Open(); int result = command.ExecuteNonQuery(); if (result != 0) { return(true); } }; return(false); }