ASP.NET core, send more data with formData
Hi, I want to be sending an string as well as the other data im sending. Is that possible? Its uploading an img to azure storage account but I need to add an more info to the fileEntity so that I can get the filepath to the specific product, therefore I need to send the productId from the razor page to the other project where I upload the file.
24 Replies
You can have as much stuff in the form data as you want
Just create some class/record/whatever to bind the action to, and have it contain an
IFormFile
property
thanks, but I dont understand where to put it
<script>
const handleSubmit = async (e) => {
const status = document.getElementById("status");
status.innerHTML = "";
const formData = new FormData();
formData.append("file", e.target["file"].files[0]);
//here I would like to be adding a string that I can get from my fileUpload function
const res = await fetch("https://fileprovider-fixo.azurewebsites.net/api/Upload?code=5SrFWL5wQu0H1nPZ8JdATKlnyWVBLzAhwPnSjSHn8N2RAzFu1W-3fQ%3D%3D&containerName=products", {
method: "post",
body: formData,
})
if (res.status == 200) {
const result = await res.json();
} else {
status.innerHTML =
Något gick snett med bilduppladdningen.
}
}
</script>
[Function("Upload")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req)
{
try
{
if(req.Form.Files["file"] is IFormFile file)
{
var containerName = !string.IsNullOrEmpty(req.Query["containerName"]) ? req.Query["containerName"].ToString() : "files";
var fileEntity = new FileEntity
{
FileName = _fileService.SetFileName(file),
ContentType = file.ContentType,
ContainerName = containerName
};
can I just do it like this, and where do I get the data in my Run function, formData.append("coolvalue", "testvaluelol");
Yep
In your run function, you get it from the parameter(s)
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req)
, should I remove all these parameters?
Keep the attribute if it's needed
Actually
like so? record MyCoolData(IFormFile File, string Unga, string Uga, bool Gupo, int Zipzoop);
public class Upload(ILogger<Upload> logger, FileService fileService)
{
private readonly ILogger<Upload> _logger = logger;
private readonly FileService _fileService = fileService;
[Function("Upload")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req, MyCoolData data)
Yeah
Something like that
Might need a
[FromForm]
attribute on MyCoolData data
on the record or parameters?
On the parameter
And the record should be
public
, as per the errorthanks, now I get when trying to run it. System.NullReferenceException: 'Object reference not set to an instance of an object.'
data was null.
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,[FromForm] MyCoolData data)
{
try
{
if(req.Form.Files["file"] is IFormFile file)
{
var containerName = !string.IsNullOrEmpty(req.Query["containerName"]) ? req.Query["containerName"].ToString() : "files";
var fileEntity = new FileEntity
{
FileName = _fileService.SetFileName(file),
ContentType = file.ContentType,
ContainerName = containerName
};
var testValueContent = data.TestValue;
might be something here? const formData = new FormData();
formData.append("file", e.target["file"].files[0]);
formData.append("TestValue", "contentvalue");
//here I would like to be adding a string that I can get from my fileUpload function
const res = await fetch("http://localhost:7036/api/Upload?containerName=products", {
method: "post",
body: formData,
})
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/Can you try to use that to structure your messages? Helps a lot with readability :heartowo:
Huh, seems AZ functions don't support parameter binding the same way regular ASP Core does
Seems form fields are accessible via
req.Form["field"]
So
You can make a proper binder though, apparently
https://stackoverflow.com/a/60284398/6042255Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
im not sure
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
its working! thanks a lot
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/