Hi,
I'm trying to use an Upload component on a .Net Core 2.2 Razor Pages project, I have a form to edit a record, where I would like to add a file as a an attachment.
I've been trying 2 difference methods to achieve this.
Method 1 - To a Controller
I've been able to get the below to upload a file to a controller, where I can convert to a byte array to eventually try and save to a database.
Component
@(Html.Kendo().Upload()
.Name("files")
.Async(a => a
.Save("SaveAsync", "Upload")
.Remove("Remove", "Upload")
)
.Validation(validation => validation.AllowedExtensions(new string[] { ".gif", ".jpg", ".png" }))
)
Controller
public async Task<
ActionResult
> SaveAsync(IEnumerable<
IFormFile
> files)
{
if (files != null)
{
foreach (var file in files)
{
if (file.Length > 0)
{
using (var ms = new MemoryStream())
{
await file.CopyToAsync(ms);
var fileBytes = ms.ToArray();
string s = Convert.ToBase64String(fileBytes);
}
}
}
}
return Content("");
}
Problem: I don't know how pass the records ID when the files are passed to the controller, so I can save the Byte Array to the correct record. How do I include a parameter?
Method 2 - Via Code Behind (Ideal)
The ideal method as this is a razor pages app, is to pass the files to a handler on the code behind, so I have the Record ID available, I've moved the SaveAsync Task to the Razor page code behind, but I can't get it to trigger when the file is uploaded.
So far, I've tried the below to call the OnPostSaveAsync Handler
@(Html.Kendo().Upload()
.Name("files")
.Async(a => a
.SaveUrl("/Case/Edit?handler=SaveAsync")
.Remove("Remove", "Upload")
)
.Validation(validation => validation.AllowedExtensions(new string[] { ".gif", ".jpg", ".png" }))
)
public async Task<
ActionResult
> OnPostSaveAsync(IEnumerable<
IFormFile
> files)
{
if (files != null)
{
foreach (var file in files)
{
if (file.Length > 0)
{
using (var ms = new MemoryStream())
{
await file.CopyToAsync(ms);
var fileBytes = ms.ToArray();
string s = Convert.ToBase64String(fileBytes);
}
}
}
}
return Content("");
}
Problem: It doesn't trigger the handler.. Can you see what I'm missing/done wrong? If passing the razor code behind isn't possible, how would I pass a parameter of the record ID to the controller, using the MVC method?
Thanks in advance for any help on this, we've only recently purchased DevCraft and I'm hoping it's something simple!
Mark
Hi Mark,
I am trying to make a call on Save nut its not getting call to code behind. I simply copy paste your code that is defined in Method One. Not sure if I'm missing anything.
Hello Mark,
To request the ActionMethod of the Controller you need to configure their names to match. Our Chunk Upload Demo showcases a more in depth example of the Controller configuration.
In addition if are you using Razor Pages then to reach the PageModel's handlers you would need to set up the Urls of the Save and Remove configuration methods like Mark's Method 2:
... .SaveUrl("/Upload?handler=SaveAsync") .RemoveUrl("/Upload?handler=Remove") ...
If the information above isn't helpful to solve the experienced issue please consider sharing some additional details about the configuration of the Upload Component on your side.
Thank you.