ســلام
برای آپلود عکس مطابق با آموزشی این کنترلر رو ساختم
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
namespace imageuploadapi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ImageuploadController : ControllerBase
{
public static IWebHostEnvironment _environment;
public ImageuploadController (IWebHostEnvironment environment)
{
_environment = environment;
}
public class Fileuploadapi
{
public IFormFile files { get; set; }
}
[HttpPost]
public async Task<string> Post(Fileuploadapi objfile)
{
try
{
if (objfile.files.Length > 0)
{
if (!Directory.Exists(_environment.WebRootPath + "\\upload\\"))
{
Directory.CreateDirectory(_environment.WebRootPath + "\\upload\\");
}
using (FileStream fileStream = System.IO.File.Create(_environment.WebRootPath + "\\upload\\" + objfile.files.FileName))
{
objfile.files.CopyTo(fileStream);
fileStream.Flush();
return "\\upload\\" + objfile.files.FileName;
}
}
else
{
return "Failed";
}
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
}
}
و این هم فایل Startup
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace imageuploadapi
{
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
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseStaticFiles();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run(async (context) => {
await context.Response.WriteAsync("could not find Anything");
});
}
}
}
و این هم شات از ارسال فایل با نرم افزار Postman