Image upload in .NET Core Web API

21,399

Solution 1

[HttpPost("UploadFile")]
 public async Task<string> UploadFile([FromForm] IFormFile file)
    {
        string fName = file.FileName;
        string path = Path.Combine(hostingEnvironment.ContentRootPath, "Images/" + file.FileName);
        using (var stream = new FileStream(path, FileMode.Create))
        {
            await file.CopyToAsync(stream);
        }
        return file.FileName; 
    }

You can use this in your controller class. Don't need to create a FileUploadAPi class.

Solution 2

Remove these lines:

app.Run(async (context) =>
{
    await context.Response.WriteAsync("Controller didn't find anything!");
});

You're short-circuiting every request and just returning "Controller didn't find anything!" with that. Nothing ever even makes it into the pipeline.

Share:
21,399
User8686
Author by

User8686

Updated on September 14, 2020

Comments

  • User8686
    User8686 over 3 years

    I'm developing a Web API with .Net Core, where I need to allow a client to upload a list of files (mostly images) and save them to the server.

    Im using ASP.NET Core 3.0

    This is my startup file Startup.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    using Microsoft.AspNetCore.Http;
    
    
    namespace ImageUplaodDemo
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
    
                app.UseHttpsRedirection();
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }
    }
    

    This is my ImageController file ImageController.cs:

    using System;
    using System.IO;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    namespace ImageUploadDemo.Controllers
    {
        [Route("api/[controller]")]
        public class ImageController : ControllerBase
        {
            public static IHostingEnvironment _environment;
            public ImageController(IHostingEnvironment environment)
            {
                _environment = environment;
            }
            public class FIleUploadAPI
            {
                public IFormFile files { get; set; }
            }
            [HttpPost]
            public async Task<string> Post(FIleUploadAPI files)
            {
                if (files.files.Length > 0)
                {
                    try
                    {
                        if (!Directory.Exists(_environment.WebRootPath + "\\uploads\\"))
                        {
                            Directory.CreateDirectory(_environment.WebRootPath + "\\uploads\\");
                        }
                        using (FileStream filestream = System.IO.File.Create(_environment.WebRootPath + "\\uploads\\" + files.files.FileName))
                        {
                            files.files.CopyTo(filestream);
                            filestream.Flush();
                            return "\\uploads\\" + files.files.FileName;
                        }
                    }
                    catch (Exception ex)
                    {
                        return ex.ToString();
                    }
                }
                else
                {
                    return "Unsuccessful";
                }
            }
        }
    }
    

    Im trying to upload a image using POSTMAN by POST operation. I assigned REQUEST to POST and im using form-data under Body tag and assigned KEY as file to upload a file from the desktop. But im getting the following error.

    I have tried all possible but been getting the same error...

    Check through the code once and there any modification need to be done on the code or any modifications on postman....

    {
        "type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
        "title": "Unsupported Media Type",
        "status": 415,
        "detail": null,
        "instance": null,
        "extensions": {
            "traceId": "|2c45b532-43521b159e7b734f."
        }
    }
    
  • User8686
    User8686 over 4 years
    Yes..it solved the issue...but in postman when im trying to upload a jpef file it is returning json data showing as below....and the image is not getting uploaded to wwwroot/uploads. { "type": "tools.ietf.org/html/rfc7231#section-6.5.13", "title": "Unsupported Media Type", "status": 415, "detail": null, "instance": null, "extensions": { "traceId": "|91d2286c-4581ab206339b789." } } It is indicating that the payload is in a format not supported by the method or target resource.
  • User8686
    User8686 over 4 years
    Yes...I have sent the right body in post request. I got a json response in Postman as { "type": "tools.ietf.org/html/rfc7231#section-6.5.13", "title": "Unsupported Media Type", "status": 415, "detail": null, "instance": null, "extensions": { "traceId": "|91d2286c-4581ab206339b789." } } It is indicating that the payload is in a format not supported by the method or target resource. How to rectify this...so that the image gets uploaded in the server?
  • Bjego
    Bjego over 4 years
    Have you tried to set postman to send the file encoded as form-data? stackoverflow.com/questions/46895523/…
  • User8686
    User8686 over 4 years
    Yes....i have done it...postman was set under body tag as form data and i uploaded the jpeg image file from desktop...but after hitting SEND it was showing 415 error. It is showing that the format is not supported.
  • Bjego
    Bjego over 4 years
    I'm not an postman expert, but have you tried to use IFormFile as parameter instead of your Model and have you added [FromForm] ? Maybe this is read worthy: stackoverflow.com/questions/44538772/…
  • User8686
    User8686 over 4 years
    There is no need to use [FromForm] to get form data in controller. Framework automatically convert form data to model.
  • User8686
    User8686 over 4 years
    what is "hostingEnvironment" over there. It doesnt exist in the current context.
  • Kushan Hansika
    Kushan Hansika over 4 years
    private IHostingEnvironment hostingEnvironment;
  • Ahmed Abd Elmoniem
    Ahmed Abd Elmoniem over 3 years
    There will be a need to create a separate Upload Class if more than one action/controller needs to upload an image, to reduce code repeating.
  • Admin
    Admin over 3 years
    The official docs warns against using the user provided filename, as an attacker could provide a malicious filename. You can use string untrustedFileName = Path.GetFileName(pathName); to remove paths from the filename, string myEncodedString = HttpUtility.HtmlEncode(myString); to HTML encode it. And use var filePath = Path.GetTempFileName(); to get a temporary file name and directory. Source: docs.microsoft.com