public async Task <HttpResponseMessage> UploadAsync() { try { ItemResponse <int> response = new ItemResponse <int>(); HttpPostedFile postedFile = HttpContext.Current.Request.Files[0]; int ConcernId = Int32.Parse(GetUntilOrEmpty(postedFile.FileName)); FileUploadAddRequest model = new FileUploadAddRequest { ConcernId = ConcernId, FileName = postedFile.FileName, Size = postedFile.ContentLength, Type = postedFile.ContentType, ModifiedBy = HttpContext.Current.User.Identity.IsAuthenticated ? HttpContext.Current.User.Identity.Name : "anonymous" }; string contentType = Request.Content.Headers.ContentType.MediaType; serverFileName = string.Format("{0}_{1}{2}", Path.GetFileNameWithoutExtension(postedFile.FileName), Guid.NewGuid().ToString(), Path.GetExtension(postedFile.FileName)); model.ServerFileName = serverFileName; await SavePostedFile(postedFile); response.Item = await fileService.Insert(model); return(Request.CreateResponse(HttpStatusCode.OK, response)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public async Task <int> Insert(FileUploadAddRequest model) { int id = 0; this.DataProvider.ExecuteNonQuery( "User_Files_Insert", inputParamMapper : delegate(SqlParameterCollection paramCol) { SqlParameter parm = new SqlParameter(); parm.ParameterName = "@Id"; parm.SqlDbType = System.Data.SqlDbType.Int; parm.Direction = System.Data.ParameterDirection.Output; paramCol.Add(parm); paramCol.AddWithValue("@FileName", model.FileName); paramCol.AddWithValue("@Size", model.Size); paramCol.AddWithValue("@Type", model.Type); paramCol.AddWithValue("@SystemFileName", model.SystemFileName); paramCol.AddWithValue("@ModifiedBy", model.ModifiedBy); }, returnParameters : delegate(SqlParameterCollection paramCol) { id = (int)paramCol["@Id"].Value; } ); return(id); }
public async Task <int> Insert(FileUploadAddRequest model) { int id = 0; using (SqlConnection conn = new SqlConnection(connString)) { await conn.OpenAsync(); using (SqlCommand cmd = new SqlCommand("UploadedFile_Insert", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@ConcernId", model.ConcernId); cmd.Parameters.AddWithValue("@FileName", model.FileName); cmd.Parameters.AddWithValue("@Size", model.Size); cmd.Parameters.AddWithValue("@Type", model.Type); cmd.Parameters.AddWithValue("@SystemFileName", model.ServerFileName); cmd.Parameters.AddWithValue("@ModifiedBy", model.ModifiedBy); SqlParameter parm = new SqlParameter("@Id", SqlDbType.Int); parm.Direction = ParameterDirection.Output; cmd.Parameters.Add(parm); await cmd.ExecuteNonQueryAsync().ContinueWith(_ => conn.Close()); id = (int)cmd.Parameters["@Id"].Value; } } return(id); }
public async Task <HttpResponseMessage> DeleteAsync(int id) { try { FileUploadService svc = new FileUploadService(); FileUploadAddRequest model = new FileUploadAddRequest(); model = svc.SelectById(id); var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/upload"), model.SystemFileName); try { var deleteObjectRequest = new DeleteObjectRequest { BucketName = bucketname, Key = model.SystemFileName }; await awsS3Client.DeleteObjectAsync(deleteObjectRequest); } catch (AmazonS3Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } if (File.Exists(fileSavePath)) { File.Delete(fileSavePath); } svc.Delete(id); SuccessResponse resp = new SuccessResponse(); return(Request.CreateResponse(HttpStatusCode.OK, resp)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public async Task <IActionResult> UploadAsync(IFormFile files) { try { ItemResponse <int> response = new ItemResponse <int>(); FileUploadAddRequest model = new FileUploadAddRequest(); model.FileName = files.FileName; model.Size = (int)files.Length; model.Type = files.ContentType; model.ModifiedBy = "anonymous"; serverFileName = string.Format("{0}_{1}{2}", Path.GetFileNameWithoutExtension(files.FileName), Guid.NewGuid().ToString(), Path.GetExtension(files.FileName)); model.SystemFileName = serverFileName; // Saving File to AWS S3 Folder try { TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USWest1)); TransferUtilityUploadRequest request = new TransferUtilityUploadRequest(); string keyName = serverFileName; string bucketName = "ed-projects"; request.BucketName = bucketName; request.Key = keyName; request.InputStream = files.OpenReadStream(); fileTransferUtility.Upload(request); } catch (Exception ex) { return(BadRequest(ex.Message)); } response.Item = await fileService.Insert(model); return(Ok(response)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public HttpResponseMessage UploadFile() { var httpPostedFile = HttpContext.Current.Request.Files[0]; string fileName = Path.GetFileNameWithoutExtension(httpPostedFile.FileName); string extension = Path.GetExtension(httpPostedFile.FileName); var newGuid = Guid.NewGuid().ToString(""); var newfileName = fileName + "_" + newGuid + extension; Stream st = httpPostedFile.InputStream; try { if (httpPostedFile != null) { TransferUtility utility = new TransferUtility(awsS3Client); TransferUtilityUploadRequest request = new TransferUtilityUploadRequest(); request.BucketName = bucketname; request.Key = newfileName; request.InputStream = st; utility.Upload(request); //File Streamed to AWS } if (ModelState.IsValid) { FileUploadService svc = new FileUploadService(); FileUploadAddRequest model = new FileUploadAddRequest(); model.FileTypeId = 1; model.UserFileName = fileName; model.SystemFileName = newfileName; model.Location = "https://sabio-training.s3.us-west-2.amazonaws.com/Test/" + newfileName; int?id = svc.Insert(model); ItemResponse <int?> resp = new ItemResponse <int?>(); resp.Item = id; return(Request.CreateResponse(HttpStatusCode.OK, resp)); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public FileUploadAddRequest SelectById(int id) { FileUploadAddRequest viewModel = new FileUploadAddRequest(); string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); if (conn.State == System.Data.ConnectionState.Open) { string sqlCmd = "FileUpload_SelectById"; using (SqlCommand cmd = new SqlCommand(sqlCmd, conn)) { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Id", id); SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); if (reader.Read()) { int index = 0; viewModel.Id = reader.GetInt32(index++); viewModel.FileTypeId = reader.GetInt32(index++); viewModel.UserFileName = reader.GetString(index++); viewModel.SystemFileName = reader.GetString(index++); viewModel.Location = reader.GetString(index++); return(viewModel); } else { return(null); } } } else { return(null); } } }
public int?Insert(FileUploadAddRequest model) { string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); if (conn.State == System.Data.ConnectionState.Open) { string sqlCmd = "FileUpload_Insert"; using (SqlCommand cmd = new SqlCommand(sqlCmd, conn)) { cmd.CommandType = System.Data.CommandType.StoredProcedure; SqlParameter parm = new SqlParameter(); parm.SqlDbType = System.Data.SqlDbType.Int; parm.Direction = System.Data.ParameterDirection.Output; parm.ParameterName = "@Id"; cmd.Parameters.Add(parm); cmd.Parameters.AddWithValue("@FileTypeId", model.FileTypeId); cmd.Parameters.AddWithValue("@UserFileName", model.UserFileName); cmd.Parameters.AddWithValue("@SystemFileName", model.SystemFileName); cmd.Parameters.AddWithValue("@Location", model.Location); cmd.ExecuteNonQuery(); int id = (int)cmd.Parameters["@Id"].Value; return(id); } } else { return(null); } } }