In the new development model ASP.NET Core, whether to use the Interface
Iformfile who has the responsibility to work with the input
of the kind file
.
The problem that is happening is that the value should be a list and not 1 element, because its form configuration in the input
is with multiple
, example: List<IFormFile>
.
Basic example:
Html
<form role="form" asp-action="Upload" enctype="multipart/form-data">
<input type="file" name="id" id="id" multiple />
<input type="submit" value="Upload" />
</form>
Controller
public async Task<IActionResult> Upload(List<IFormFile> id)
being a factor also very important the name of input
is the same name placed in the folder that will receive the files, example, you placed id
in the name of input
should follow the same name for the controller
.
How you will receive the values being that your configuration was in a list?
I put together a minimal example to illustrate this, where this input
can send multiple files to controller
:
Xml
<?xml version="1.0" encoding="utf-8" ?>
<root>
<cliente>
<id>1</id>
<nome>stackoverflow</nome>
</cliente>
</root>
Html
<form role="form" asp-action="Upload" enctype="multipart/form-data">
<input type="file" name="id" id="id" multiple />
<input type="submit" value="Upload" />
</form>
Controller
[AcceptVerbs("POST", "GET")]
public async Task<IActionResult> Upload(List<IFormFile> id)
{
if (Request.Method.Equals("POST"))
{
foreach (var formFile in id)
{
if (formFile.Length > 0)
{
using (MemoryStream str = new MemoryStream())
{
formFile.CopyTo(str);
str.Position = 0;
var xml = (from x in XDocument.Load(str).
Descendants("cliente")
let _id = x.Element("id").Value
let _nm = x.Element("nome").Value
select new
{
Id = _id,
Nome = _nm
})
.FirstOrDefault();
}
}
}
}
return View();
}
In the example case I sent only one, but with the same layout of Xml
if you can send several, if you prefer.
Remember that if it is only 1 file at a time just take the part of the collection as follows:
Xml
<?xml version="1.0" encoding="utf-8" ?>
<root>
<cliente>
<id>1</id>
<nome>stackoverflow</nome>
</cliente>
</root>
Html
<form role="form" asp-action="Upload" enctype="multipart/form-data">
<input type="file" name="id" id="id" />
<input type="submit" value="Upload" />
</form>
Controller
[AcceptVerbs("POST", "GET")]
public async Task<IActionResult> Upload(IFormFile id)
{
if (Request.Method.Equals("POST"))
{
if (id.Length > 0)
{
using (MemoryStream str = new MemoryStream())
{
await id.CopyToAsync(str);
str.Position = 0;
var xml = (from x in XDocument.Load(str).
Descendants("cliente")
let _id = x.Element("id").Value
let _nm = x.Element("nome").Value
select new
{
Id = _id,
Nome = _nm
})
.FirstOrDefault();
}
}
}
return View();
}
that is, in the input
remove the setting multiple
and in the controller
leave only the interface Iformfile
References
Ask the question an XML sample to help understand the problem.
– Pagotti
Good Afternoon Pagotti, as said the issue is not with xml but with reading by Xdocument that does not accept the way I receive the file in the controller, but anyway it is a simple NFE xml accurate to read the date of issue.
– Thiago Motta Barboza
Okay. If you use
param.OpenReadStream()
in theLoad
inoperative?– Pagotti
I’ll try and tell you that option I haven’t tried yet
– Thiago Motta Barboza
As is your form, can make available?
– novic
@Thiagomottabarboza you better add the c# tag in your question, it will make it easier to find people who can help you.
– Genos
@Pagotti Openreadstream is recognized but it is not accepted to query in the xml used in the following code: var dataXml = Xdocument.Load(xml.Openreadstream()) . Descendants("ide") . First() . Element("dhEmi") . Value;
– Thiago Motta Barboza
@Virgilionovic my form is simple, I have not used anything different follows added the code in the post
– Thiago Motta Barboza
@Thiagomottabarboza I made the solution is that you are doing wrong in some points so I made two examples in the answer, if you can use any of the two reflecting on your ai... !!!
– novic