Skip to content

Instantly share code, notes, and snippets.

@ZhenDeng
Last active May 22, 2019 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZhenDeng/6a88ae022287d00a1a9bcbbcbae0f047 to your computer and use it in GitHub Desktop.
Save ZhenDeng/6a88ae022287d00a1a9bcbbcbae0f047 to your computer and use it in GitHub Desktop.
1. in startup.cs
services.AddSingleton<IFileProvider>(
new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")));
2. in controller
#region Upload Users
[HttpPost("UploadUsers"), DisableRequestSizeLimit]
[NoCache]
[ProducesResponseType(typeof(ApiResponse), 200)]
[ProducesResponseType(typeof(ApiResponse), 400)]
public IActionResult UploadUsers()
{
try
{
var httpRequest = HttpContext.Request;
if (httpRequest.Form.Files.Count > 0)
{
var uploadFile = httpRequest.Form.Files[0]; // get the uploaded file
if (uploadFile != null && uploadFile.Length > 0)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", @"ImportUsers.xlsx");
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
using (var stream = new FileStream(path, FileMode.Create))
{
uploadFile.CopyTo(stream);
stream.Close();
}
FileInfo file = new FileInfo(path);
using (ExcelPackage package = new ExcelPackage(file))
{
ExcelWorksheet workSheet = package.Workbook.Worksheets["Sheet1"];
int totalRows = workSheet.Dimension.Rows;
List<User> userList = new List<User>();
for (int i = 3; i <= totalRows; i++)
{
if (workSheet.Cells[i, 1].Value != null)
{
StringBuilder builder = new StringBuilder();
builder.Append(share.RandomString(4, true));
builder.Append(share.RandomNumber(1000, 9999));
builder.Append(share.RandomString(2, false));
userList.Add(new User()
{
Account = workSheet.Cells[i, 1].Value == null ? "" : workSheet.Cells[i, 1].Value.ToString(),
Password = builder.ToString(),
BussinessName = workSheet.Cells[i, 2].Value == null ? "" : workSheet.Cells[i, 2].Value.ToString(),
BillingStreetNumber = workSheet.Cells[i, 3].Value == null ? "" : workSheet.Cells[i, 3].Value.ToString(),
BillingAddressLine = workSheet.Cells[i, 4].Value == null ? "" : workSheet.Cells[i, 4].Value.ToString(),
BillingSuburb = workSheet.Cells[i, 5].Value == null ? "" : workSheet.Cells[i, 5].Value.ToString(),
BillingState = workSheet.Cells[i, 6].Value == null ? "" : workSheet.Cells[i, 6].Value.ToString(),
BillingPostCode = workSheet.Cells[i, 7].Value == null ? "" : workSheet.Cells[i, 7].Value.ToString(),
ShippingStreetNumber = workSheet.Cells[i, 8].Value == null ? "" : workSheet.Cells[i, 8].Value.ToString(),
ShippingAddressLine = workSheet.Cells[i, 9].Value == null ? "" : workSheet.Cells[i, 9].Value.ToString(),
ShippingSuburb = workSheet.Cells[i, 10].Value == null ? "" : workSheet.Cells[i, 10].Value.ToString(),
ShippingState = workSheet.Cells[i, 11].Value == null ? "" : workSheet.Cells[i, 11].Value.ToString(),
ShippingPostCode = workSheet.Cells[i, 12].Value == null ? "" : workSheet.Cells[i, 12].Value.ToString(),
Phone = workSheet.Cells[i, 13].Value == null ? "" : workSheet.Cells[i, 13].Value.ToString(),
CompanyPhone = workSheet.Cells[i, 14].Value == null ? "" : workSheet.Cells[i, 14].Value.ToString(),
Mobile = workSheet.Cells[i, 15].Value == null ? "" : workSheet.Cells[i, 15].Value.ToString(),
Email = workSheet.Cells[i, 16].Value == null ? "" : workSheet.Cells[i, 16].Value.ToString()
});
}
}
package.Stream.Close();
package.Dispose();
trojantradingDbContext.Users.AddRange(userList);
trojantradingDbContext.SaveChanges();
trojantradingDbContext.Dispose();
}
return Ok(new ApiResponse
{
Status = "success",
Message = " PRICE LIST Pdf file uploaded."
});
}
}
return Ok(new ApiResponse
{
Status = "fail",
Message = "Upload file is empty"
});
}
catch (Exception ex)
{
return Ok(new ApiResponse
{
Status = "fail",
Message = ex.Message
});
}
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment