Click here to Skip to main content
16,017,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am using XAMARIN FORM.
I Can read the file after take the picture.
I can see the file.FileName
But no parameters is passed to the WEB API.
I am completly lost, i really do not know whey
C#
private async void TakePhoto_Clicked(object sender, EventArgs e)
 {
     var file = await MediaPicker.CapturePhotoAsync();
     var content = new MultipartFormDataContent();
     content.Add(new StreamContent(await file.OpenReadAsync()), "file", file.FileName);
     string url = "https://10.0.2.2:7254/api/GetData/Upload/";
     client.BaseAddress = new Uri(url);       
     var response = await client.PostAsync("", content);
     if (response.IsSuccessStatusCode)
           
  }

Web API
[HttpPost]
[Route("Upload")]
public async Task<string> Upload(  List <IFormFile> files)    
{
    try
    {
        foreach (var formFile in files)
        {
           if (formFile.Length > 0)
           {
              var fileName =  formFile.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();
              var filePath = Path.Combine(Directory.GetCurrentDirectory(), "imagenes", fileName);
                       
               using (var stream = System.IO.File.Create(filePath))
               {
                  await formFile.CopyToAsync(stream);
               }


What I have tried:

I had read notes here and other site
Posted
Comments
[no name] 5-Apr-23 2:46am    
the split() function divide an item to an Array.
it breaks a big string in subparts ( belonging to the choosen separator )


when you use 'lastorDefault', you select the last index of the retrieve Array.

and after the Path.combine() a 'void' field make this function in error.

the error comes from the line "var filename = ...."






to debug and find the error :
- check the value about "filename"
- rewrite the line "var filename= ".

when you have a faulty code :
- trace your vars
- check their values and types
Luis M. Rojas 5-Apr-23 11:02am    
Well, if you see these lines:
content.Add(new StreamContent(await file.OpenReadAsync()), "file", file.FileName);
string url = "https://10.0.2.2:7254/api/GetData/Upload/";
client.BaseAddress = new Uri(url);
var response = await client.PostAsync("", content);
In this line:
content.Add(new StreamContent(await file.OpenReadAsync()), "file", file.FileName);
I can read and see the file.FileName
But in the Web Api, in this line: public async Task<string> Upload( List <iformfile> files)
files do not recive any value, i mean: it is EMPLY,
How do i pass the value from the program and pass it to the web api.
I did what you said.
Thanks to all

1 solution

as you want to get the name.extension of the string you have :

Path.GetFileName Method (System.IO) | Microsoft Learn[^]

var filename = path.getfilename(formFile)
// to break the big string and get the filename and extension only
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900