Upload files to ASP.NET MVC Web api controller

I want to create a WEB API that downloads a file from this client on Azure. For this, I know that I can use a class like: MultipartFormDataStreamProvider (not sure if this class works for Azure)

I want this API to be accessible from various applications. For starters, a simple .NET application. But my question is: can I use the same API to handle file downloads, for example, from an Android application, a Windows 8 application, etc.

If this is not possible, then all of these applications require a separate API?

My idea of ​​the API is that it can be used in various applications to provide the required functionality. But in this case, the classes needed to load the file will limit its use.

+3
source share
2 answers

@nikhil pinto: ASP.NET Web API helps you create a REST-Web service that can be used by any client that supports an HTTP request. I found this article here.

http://hintdesk.com/android-upload-files-to-asp-net-web-api-service

very good, because it illustrates how we can call a web service from different clients. Just check it out.

+3
source

Api web code for image upload

[System.Web.Http.AcceptVerbs("POST")]
        public async Task<object> AddAttachment()
        {
            if (!Request.Content.IsMimeMultipartContent("form-data"))
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
            }

            NamedMultipartFormDataStreamProvider streamProvider = new NamedMultipartFormDataStreamProvider(
                HttpContext.Current.Server.MapPath("~/App_Data/"));

            await Request.Content.ReadAsMultipartAsync(streamProvider);
            string path = streamProvider.FileData.Select(entry => entry.LocalFileName).First();
            byte[] imgdata = System.IO.File.ReadAllBytes(path);

            return new
            {
                FileNames = streamProvider.FileData.Select(entry => entry.LocalFileName),

            };
        }
+1
source

All Articles