C
C#17mo ago
Bordin

✅ file path not saving properly with id

[HttpPost("")]
public async Task<ActionResult<CompanyResource>> CreateCompany([FromForm] SaveCompanyResource saveCompanyResource)
{
var validator = new SaveCompanyResourceValidator();
var validationResult = await validator.ValidateAsync(saveCompanyResource);
var uploadsFolderPath = "Uploads/";
if (!validationResult.IsValid)
return BadRequest(validationResult.Errors); // this needs refining, but for demo it is ok

var companyToCreate = _mapper.Map<SaveCompanyResource, Company>(saveCompanyResource);
if (saveCompanyResource.Logo != null && saveCompanyResource.Logo.Length > 0)
{
// Generate the filename using the CompanyId or any other unique identifier
var fileName = companyToCreate.CompanyId.ToString() + Path.GetExtension(saveCompanyResource.Logo.FileName);
var filePath = Path.Combine(uploadsFolderPath, fileName);

using (var stream = new FileStream(filePath, FileMode.Create))
{
await saveCompanyResource.Logo.CopyToAsync(stream);
}

// Set the file path in the companyToCreate object to be "Uploads/companyId.jpg"
companyToCreate.Logo = filePath.ToString();
}
var newCompany = await _companyService.CreateCompany(companyToCreate);

var company = await _companyService.GetCompanyById(newCompany.CompanyId);

var companyResource = _mapper.Map<Company, CompanyResource>(company);

return Ok(companyResource);
}
[HttpPost("")]
public async Task<ActionResult<CompanyResource>> CreateCompany([FromForm] SaveCompanyResource saveCompanyResource)
{
var validator = new SaveCompanyResourceValidator();
var validationResult = await validator.ValidateAsync(saveCompanyResource);
var uploadsFolderPath = "Uploads/";
if (!validationResult.IsValid)
return BadRequest(validationResult.Errors); // this needs refining, but for demo it is ok

var companyToCreate = _mapper.Map<SaveCompanyResource, Company>(saveCompanyResource);
if (saveCompanyResource.Logo != null && saveCompanyResource.Logo.Length > 0)
{
// Generate the filename using the CompanyId or any other unique identifier
var fileName = companyToCreate.CompanyId.ToString() + Path.GetExtension(saveCompanyResource.Logo.FileName);
var filePath = Path.Combine(uploadsFolderPath, fileName);

using (var stream = new FileStream(filePath, FileMode.Create))
{
await saveCompanyResource.Logo.CopyToAsync(stream);
}

// Set the file path in the companyToCreate object to be "Uploads/companyId.jpg"
companyToCreate.Logo = filePath.ToString();
}
var newCompany = await _companyService.CreateCompany(companyToCreate);

var company = await _companyService.GetCompanyById(newCompany.CompanyId);

var companyResource = _mapper.Map<Company, CompanyResource>(company);

return Ok(companyResource);
}
I am creating a file with path Uploads/companyId.png However. The companyId is 0 because i am creating newCompany after i create the path. And if i created it before it saves as Microsoft.AspNetCore.http... And i guess that's because i created the company with no path. any idea how i can fix this? its so confusing
7 Replies
Saber
Saber17mo ago
why not do what the comment says and to use a unique identifier like a guid instead because you don't know the companyid until it's been created
Bordin
BordinOP17mo ago
What do you mean? I don't have any other unique identifiers
Saber
Saber17mo ago
use a unique identifier like a guid instead lol
Bordin
BordinOP17mo ago
Sorry don't know what guid mean
Saber
Saber17mo ago
thankfully our friend google can probably help you with that
Bordin
BordinOP17mo ago
Mhmm googled it. Looks like what i need. Thank you worked!
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server