Giter Site home page Giter Site logo

amanuelab / uploadfiletoserver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from houssemdellai/uploadfiletoserver

0.0 0.0 0.0 2.21 MB

Upload a file from Xamarin Forms app to ASP.NET web application.

C# 44.58% CSS 0.67% ASP 0.03% HTML 1.30% JavaScript 53.41%

uploadfiletoserver's Introduction

UploadFileToServer

Upload a file from Xamarin Forms app to ASP.NET web application.

This repo shows how to upload a file from Xamarin Forms app:

    private MediaFile _mediaFile;

    private async void UploadFile_Clicked(object sender, EventArgs e)
    {
        var content = new MultipartFormDataContent();

        content.Add(new StreamContent(_mediaFile.GetStream()),
            "\"file\"",
            $"\"{_mediaFile.Path}\"");

        var httpClient = new HttpClient();

        var uploadServiceBaseAddress = "http://uploadtoserver.azurewebsites.net/api/Files/Upload";
        //"http://localhost:12214/api/Files/Upload";

        var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);

        RemotePathLabel.Text = await httpResponseMessage.Content.ReadAsStringAsync();
    }

And shows to get and save that file with an ASP.NET application:

public class UploadsController : 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 exception)
        {
            return exception.Message;
        }

        return "no files";
    }
}

uploadfiletoserver's People

Contributors

houssemdellai avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.