Skip to content

Commit

Permalink
Add upload file api
Browse files Browse the repository at this point in the history
  • Loading branch information
yenilikci committed Aug 8, 2021
1 parent ab72367 commit 3ba6593
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 1 deletion.
Binary file modified angular-dotnetcore-postgresql/api/.vs/api/v16/.suo
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using api.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -16,10 +18,12 @@ namespace api.Controllers
public class EmployeeController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _env;

public EmployeeController(IConfiguration configuration)
public EmployeeController(IConfiguration configuration, IWebHostEnvironment env)
{
_configuration = configuration;
_env = env;
}

[HttpGet]
Expand Down Expand Up @@ -136,5 +140,29 @@ delete from employee
return new JsonResult("Deleted Successfully");
}

[Route("SaveFile")]
[HttpPost]
public JsonResult SaveFile()
{
try
{
var httpRequest = Request.Form;
var postedFile = httpRequest.Files[0];
string filename = postedFile.FileName;
var physicalPath = _env.ContentRootPath + "/Photos/" + filename;

using (var stream = new FileStream(physicalPath, FileMode.Create))
{
postedFile.CopyTo(stream);
}
return new JsonResult(filename);

}
catch (Exception)
{

return new JsonResult("anonymous.png");
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions angular-dotnetcore-postgresql/api/api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Serialization;
using Microsoft.Extensions.FileProviders;
using System.IO;

namespace api
{
Expand Down Expand Up @@ -57,6 +59,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
endpoints.MapControllers();
});

app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Photos")),
RequestPath = "/Photos"
});
}
}
}
4 changes: 4 additions & 0 deletions angular-dotnetcore-postgresql/api/api/api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
<PackageReference Include="Npgsql" Version="5.0.7" />
</ItemGroup>

<ItemGroup>
<Folder Include="Photos\" />
</ItemGroup>


</Project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 3ba6593

Please sign in to comment.