Press ESC to close

Xamarin Uploading Photos and Videos to the Server

Hello. In today’s writing, we will talk about a topic that is on almost every major project. If you want to upload photos to our application or choose to interact with other users online after you select a photo, we need to store it in a server or database. We can do this using the API. In this writing we will do them.

First we need to write a web service. For this I will create an ASP.NET MVC project. We need to pay attention here; Note that the Web API option must be selected from the options that come up while creating the project. Then create the project. Actually, the process is very simple. We will write a few lines of code. In our project, we need to create a class by overriding the Controllers folder. Let’s put the name of this class in UploadController. You can change it as you like. This class must inherit from the ApiController class. We have to make an orientation later. With this orientation, the field will be added to the end of your step. For instance, take the domain step sezeromer.com if you need to. If I make a redirect here, it will be sezeromer.com/api/Files/Upload. When I enter this address, the function in this class will work. Then I write my codes. The other issue that needs to be taken care of here is where we are going to record our photos or videos. I specified here as the uploads folder. So I’m adding a folder called uploads to the project one by one.

public class UploadController : ApiController
    {
        [Route("api/Files/Upload")]
        public async Task<string> Post()
        {
            try
            {
                var httpRequest = HttpContext.Current.Request;
                if (httpRequest.Files.Count>0)
                {
                    foreach (string file in httpRequest.Files)
                    {
                        var postedFile = httpRequest.Files[file];
 
                        var fileName = postedFile.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();
 
                        var filePath = HttpContext.Current.Server.MapPath("~/Uploads/" + fileName);
 
                        postedFile.SaveAs(filePath);
 
                        return "/Uploads/" + fileName;
                    }
                }
            }
            catch (Exception hata)
            {
 
                return hata.Message;
            }
            return "Dosya Yok";
        }

Actually, our service is that easy. If you like this service you can stay in Azure or use it locally. After that, on the Xamarin side. I use the Xaml.Media.plugin plugin to select or draw photos in the Xamarin.Forms project. You can use whatever you want, provided it is in the MediaFile type.

To send data to our Web service, we need to create a content. After creating the content, we put the photo and the path of our photo into it. To send this content to adrese, we need to create a Client. After creating the client, we need to specify the address. We need to specify which address it is. I have mentioned above You have to write your forwarding address if you have forwarded your address since you wrote it. We send the content we created as the last transaction with the client we have created. That’s our process.

var content = new MultipartFormDataContent();
 
            content.Add(new StreamContent(_mediFile.GetStream()),
                "\"file\"",
                $"\"{_mediFile.Path}\"");
 
            var httpClient = new HttpClient();
 
            var uploadServicBaseAdress = Constants.UploadBaseAddress;
 
            var httpResponseMessage = await httpClient.PostAsync(uploadServicBaseAdress, content);

If you encounter a problem, you can reach it by comment or mail.

 

Comments (3)

Leave a Reply

Your email address will not be published. Required fields are marked *