7

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
  • 71
  • 1
  • 1
  • 4

3 Answers3

11
[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.

  • 1
    what is "hostingEnvironment" over there. It doesnt exist in the current context. – User8686 Sep 13 '19 at 10:30
  • private IHostingEnvironment hostingEnvironment; – Kushan Hansika Sep 16 '19 at 08:49
  • 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. – Ahmed Abd Elmoniem Sep 10 '20 at 11:23
  • 1
    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: [learn.microsoft.com](https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.1#upload-small-files-with-buffered-model-binding-to-physical-storage-1) – log234 Sep 11 '20 at 11:48
1

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.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • 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": "https://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 Sep 13 '19 at 04:48
0

Hey have you tried the official docus?

https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.2

and this one https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.2&tabs=visual-studio

I would try to use official sources to learn topics like this, instead of random blogs.

Have you debugged your controller? Is the IFormFile in your viewmodel already mapped?

The controller should be found automatically and respond to:

http(s)://localhost:DEVPORT/api/Image

You could add an Index Endpoint returning a string "Hello World" and you should see a Json Response , when you try to connect with your browser..

Not sure why you are doing this:

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

Did you send the right body in your post request?

Bjego
  • 665
  • 5
  • 14
  • Yes...I have sent the right body in post request. I got a json response in Postman as { "type": "https://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? – User8686 Sep 13 '19 at 04:58
  • Have you tried to set postman to send the file encoded as form-data? https://stackoverflow.com/questions/46895523/asp-core-webapi-test-file-upload-using-postman – Bjego Sep 13 '19 at 06:07
  • 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. – User8686 Sep 13 '19 at 08:46
  • 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: https://stackoverflow.com/questions/44538772/asp-net-core-form-post-results-in-a-http-415-unsupported-media-type-response – Bjego Sep 13 '19 at 09:31
  • There is no need to use [FromForm] to get form data in controller. Framework automatically convert form data to model. – User8686 Sep 13 '19 at 10:27