I am successfully uploading files to the server using the Upload component but I have a few questions/issues.
-
When I specify the value pdf/* in the Accept property, I am still seeing (and uploading) files of any type. Should this not filter for just pdf extensions?
-
The file upload code is in the UploadController (and working)
// Single file upload
[HttpPost("upload/single")]
public async Task SingleAsync(IFormFile file)
{
try
{
// Put your code here
await UploadFile(file);
return StatusCode(200);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
public async Task UploadFile(IFormFile file)
{
if (file != null && file.Length > 0)
{
var imagePath = @"\Data\TempFiles";
var uploadPath = _env.WebRootPath + imagePath;
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
var fullPath = Path.Combine(uploadPath, file.FileName);
fullUploadPath = fullPath;
using (FileStream fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write))
{
await file.CopyToAsync(fileStream);
}
}
}
I need to be able to specify a new name (without path) of the uploaded file different from the name on the client system.
Either by providing it to UploadFile or by renaming the file after UploadFile is complete in the 'Complete' event.
I haven't been able to find a way to pass another parameter to "SingleAsync" or to access IWebHostEnvironment from the Upload.Complete event.
Any assistance would be greatly appreciated.