Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using WebApiFileUploadSample.Models;
using Microsoft.EntityFrameworkCore;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApiFileUploadSample.Controllers
{
using WebApiFileUploadSample.Models;

[Route("api/[controller]")]
public class FilesController : Controller
{
Expand All @@ -20,8 +21,9 @@ public FilesController(SQLiteDbContext dbContext)
{
_dbContext = dbContext;
}

// GET: api/values
[HttpGet]
[HttpGet("Image")]
public IActionResult Get()
{
try {
Expand All @@ -35,7 +37,7 @@ public IActionResult Get()
}

// GET api/values/5
[HttpGet("api/Image/{name}")]
[HttpGet("Image/{name}")]
public IActionResult Get(string name)
{
try
Expand Down Expand Up @@ -77,7 +79,7 @@ public async Task<IActionResult> PostFile(IFormFile uploadedFile)
}

// DELETE api/values/5
[HttpDelete("api/Image/{id}")]
[HttpDelete("imageid/{id}")]
public IActionResult Delete(int id)
{
try
Expand All @@ -93,7 +95,7 @@ public IActionResult Delete(int id)
}
}
// GET api/values/5
[HttpDelete("api/Image/{name}")]
[HttpDelete("imagename/{name}")]
public IActionResult Delete(string name)
{
try
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace WebApiFileUploadSample.Controllers
{
using WebApiFileUploadSample.Models;

[Route("api/[controller]")]
public class UploadController : Controller
{
private readonly ILogger logger;
private readonly SQLiteDbContext _dbContext;
public UploadController(SQLiteDbContext dbContext, ILogger<UploadController> logger)
{
this.logger = logger;
_dbContext = dbContext;
}
// GET: api/values
[HttpGet("text")]
public IActionResult Get()
{
try
{
var list = _dbContext.Files.Select(x => new { Name = x.Name }).ToList();
return Ok(list);
}
catch (Exception ex)
{
this.logger.LogWarning("", ex);
return NotFound();
}
}
[HttpGet("download/{name}")]
public IActionResult GetDownload(string name)
{
try
{
var val = _dbContext.Files.FirstOrDefault(d => d.Name == name);
Response.ContentType = "application/octet-stream";
return Ok(new MemoryStream(Encoding.UTF8.GetBytes(val.Text))); //File(val.Text, "text/xml");
}
catch (Exception ex)
{
this.logger.LogWarning("", ex);
return NotFound();
}
}

[HttpGet("text/{name}")]
[Produces("text/xml")]
public IActionResult Get(string name)
{
try
{
var val = _dbContext.Files.FirstOrDefault(d => d.Name == name);
return Ok(new MemoryStream(Encoding.UTF8.GetBytes(val.Text))); //File(val.Text, "text/xml");
}
catch (Exception ex)
{
this.logger.LogWarning("", ex);
return NotFound();
}
}

// POST api/values
[HttpPost]
[Route("upload/{id}/")]
public async Task<IActionResult> PostFile([FromRoute]short id, IFormFile uploadedFile)
{
try
{
using (var memoryStream = new MemoryStream())
{
var val = new File();
val.Name = uploadedFile.FileName;
await uploadedFile.CopyToAsync(memoryStream);
val.Text = Encoding.UTF8.GetString(memoryStream.ToArray());
_dbContext.Files.Add(val);
_dbContext.SaveChanges();
}
// process uploaded files
// Don't rely on or trust the FileName property without validation.

return Ok();
}
catch (Exception ex)
{
this.logger.LogWarning("", ex);
return BadRequest();
}
}

// DELETE api/values/5
[HttpDelete("textid/{id}")]
public IActionResult Delete(int id)
{
try
{
var val = _dbContext.Files.FirstOrDefault(d => d.Id == id);
_dbContext.Remove(val);
_dbContext.SaveChanges();
return Ok();
}
catch (Exception ex)
{
this.logger.LogWarning("", ex);
return NotFound();
}
}
// GET api/values/5
[HttpDelete("filename/{name}")]
public IActionResult Delete(string name)
{
try
{
var val = _dbContext.Files.FirstOrDefault(d => d.Name == name);
_dbContext.Remove(val);
_dbContext.SaveChanges();
return Ok();
}
catch (Exception ex)
{
this.logger.LogWarning("", ex);
return NotFound();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ public void Apply(Operation operation, OperationFilterContext context)
});
operation.Consumes.Add("multipart/form-data");
}

// オペレーションIDはSwaggerから調べるBy{id}
if (operation.OperationId.ToLower() == "apiuploaduploadbyidpost")
{
var param = operation.Parameters[0]; // パラメータ取得By{id}部分
operation.Parameters.Clear();
operation.Parameters.Add(param);
operation.Parameters.Add(new NonBodyParameter
{
Name = "uploadedFile",
In = "formData",
Description = "Upload File",
Required = true,
Type = "file"
});
operation.Consumes.Add("multipart/form-data");
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

public DbSet<Value> Values { get; set; }

public DbSet<File> Files { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
//builder.Entity<Value>().ToTable("Values");
builder.Entity<Value>().HasKey(m => m.Id); // この場合、自動採番がデフォルトで、ID無しで登録になる。
builder.Entity<File>().HasKey(m => m.Id); // この場合、自動採番がデフォルトで、ID無しで登録になる。
base.OnModelCreating(builder);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

public DbSet<Value> Values { get; set; }

public DbSet<File> Files { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Value>().HasKey(m => m.Id);
builder.Entity<File>().HasKey(m => m.Id); // この場合、自動採番がデフォルトで、ID無しで登録になる。

base.OnModelCreating(builder);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ public class Value
public string Name { get; set; }
public byte[] Image { get; set; }
}
public class File
{
public int Id { get; set; }
public string Name { get; set; }
public string Text { get; set; }
}
}
4 changes: 2 additions & 2 deletions WebApiFileUploadSample/src/WebApiFileUploadSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public void ConfigureServices(IServiceCollection services)
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "Geo Search API",
Description = "A simple api to search using geo location in Elasticsearch",
Title = "File Store API",
Description = "A simple api to File Access",
TermsOfService = "None"
});
//options.IncludeXmlComments(pathToDoc);
Expand Down
Binary file modified WebApiFileUploadSample/src/WebApiFileUploadSample/db.sqlite
Binary file not shown.