Skip to content

Commit 2647577

Browse files
authored
Merge pull request #13 from f7q/AddUploadSample
Add Upload Sample API IISの挙動で、404.7ファイル拡張子エラーが出るのは環境次第???
2 parents d905e7b + 17abf8f commit 2647577

File tree

8 files changed

+175
-7
lines changed

8 files changed

+175
-7
lines changed

WebApiFileUploadSample/src/WebApiFileUploadSample/Controllers/FilesController.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
using System.Threading.Tasks;
66
using Microsoft.AspNetCore.Mvc;
77
using Microsoft.AspNetCore.Http;
8-
using WebApiFileUploadSample.Models;
98
using Microsoft.EntityFrameworkCore;
109

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

1312
namespace WebApiFileUploadSample.Controllers
1413
{
14+
using WebApiFileUploadSample.Models;
15+
1516
[Route("api/[controller]")]
1617
public class FilesController : Controller
1718
{
@@ -20,8 +21,9 @@ public FilesController(SQLiteDbContext dbContext)
2021
{
2122
_dbContext = dbContext;
2223
}
24+
2325
// GET: api/values
24-
[HttpGet]
26+
[HttpGet("Image")]
2527
public IActionResult Get()
2628
{
2729
try {
@@ -35,7 +37,7 @@ public IActionResult Get()
3537
}
3638

3739
// GET api/values/5
38-
[HttpGet("api/Image/{name}")]
40+
[HttpGet("Image/{name}")]
3941
public IActionResult Get(string name)
4042
{
4143
try
@@ -77,7 +79,7 @@ public async Task<IActionResult> PostFile(IFormFile uploadedFile)
7779
}
7880

7981
// DELETE api/values/5
80-
[HttpDelete("api/Image/{id}")]
82+
[HttpDelete("imageid/{id}")]
8183
public IActionResult Delete(int id)
8284
{
8385
try
@@ -93,7 +95,7 @@ public IActionResult Delete(int id)
9395
}
9496
}
9597
// GET api/values/5
96-
[HttpDelete("api/Image/{name}")]
98+
[HttpDelete("imagename/{name}")]
9799
public IActionResult Delete(string name)
98100
{
99101
try
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using System;
2+
using System.Text;
3+
using System.IO;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.AspNetCore.Http;
9+
using Microsoft.EntityFrameworkCore;
10+
using Microsoft.Extensions.Logging;
11+
12+
namespace WebApiFileUploadSample.Controllers
13+
{
14+
using WebApiFileUploadSample.Models;
15+
16+
[Route("api/[controller]")]
17+
public class UploadController : Controller
18+
{
19+
private readonly ILogger logger;
20+
private readonly SQLiteDbContext _dbContext;
21+
public UploadController(SQLiteDbContext dbContext, ILogger<UploadController> logger)
22+
{
23+
this.logger = logger;
24+
_dbContext = dbContext;
25+
}
26+
// GET: api/values
27+
[HttpGet("text")]
28+
public IActionResult Get()
29+
{
30+
try
31+
{
32+
var list = _dbContext.Files.Select(x => new { Name = x.Name }).ToList();
33+
return Ok(list);
34+
}
35+
catch (Exception ex)
36+
{
37+
this.logger.LogWarning("", ex);
38+
return NotFound();
39+
}
40+
}
41+
[HttpGet("download/{name}")]
42+
public IActionResult GetDownload(string name)
43+
{
44+
try
45+
{
46+
var val = _dbContext.Files.FirstOrDefault(d => d.Name == name);
47+
Response.ContentType = "application/octet-stream";
48+
return Ok(new MemoryStream(Encoding.UTF8.GetBytes(val.Text))); //File(val.Text, "text/xml");
49+
}
50+
catch (Exception ex)
51+
{
52+
this.logger.LogWarning("", ex);
53+
return NotFound();
54+
}
55+
}
56+
57+
[HttpGet("text/{name}")]
58+
[Produces("text/xml")]
59+
public IActionResult Get(string name)
60+
{
61+
try
62+
{
63+
var val = _dbContext.Files.FirstOrDefault(d => d.Name == name);
64+
return Ok(new MemoryStream(Encoding.UTF8.GetBytes(val.Text))); //File(val.Text, "text/xml");
65+
}
66+
catch (Exception ex)
67+
{
68+
this.logger.LogWarning("", ex);
69+
return NotFound();
70+
}
71+
}
72+
73+
// POST api/values
74+
[HttpPost]
75+
[Route("upload/{id}/")]
76+
public async Task<IActionResult> PostFile([FromRoute]short id, IFormFile uploadedFile)
77+
{
78+
try
79+
{
80+
using (var memoryStream = new MemoryStream())
81+
{
82+
var val = new File();
83+
val.Name = uploadedFile.FileName;
84+
await uploadedFile.CopyToAsync(memoryStream);
85+
val.Text = Encoding.UTF8.GetString(memoryStream.ToArray());
86+
_dbContext.Files.Add(val);
87+
_dbContext.SaveChanges();
88+
}
89+
// process uploaded files
90+
// Don't rely on or trust the FileName property without validation.
91+
92+
return Ok();
93+
}
94+
catch (Exception ex)
95+
{
96+
this.logger.LogWarning("", ex);
97+
return BadRequest();
98+
}
99+
}
100+
101+
// DELETE api/values/5
102+
[HttpDelete("textid/{id}")]
103+
public IActionResult Delete(int id)
104+
{
105+
try
106+
{
107+
var val = _dbContext.Files.FirstOrDefault(d => d.Id == id);
108+
_dbContext.Remove(val);
109+
_dbContext.SaveChanges();
110+
return Ok();
111+
}
112+
catch (Exception ex)
113+
{
114+
this.logger.LogWarning("", ex);
115+
return NotFound();
116+
}
117+
}
118+
// GET api/values/5
119+
[HttpDelete("filename/{name}")]
120+
public IActionResult Delete(string name)
121+
{
122+
try
123+
{
124+
var val = _dbContext.Files.FirstOrDefault(d => d.Name == name);
125+
_dbContext.Remove(val);
126+
_dbContext.SaveChanges();
127+
return Ok();
128+
}
129+
catch (Exception ex)
130+
{
131+
this.logger.LogWarning("", ex);
132+
return NotFound();
133+
}
134+
}
135+
}
136+
}

WebApiFileUploadSample/src/WebApiFileUploadSample/FileUploadOperation.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ public void Apply(Operation operation, OperationFilterContext context)
2626
});
2727
operation.Consumes.Add("multipart/form-data");
2828
}
29+
30+
// オペレーションIDはSwaggerから調べるBy{id}
31+
if (operation.OperationId.ToLower() == "apiuploaduploadbyidpost")
32+
{
33+
var param = operation.Parameters[0]; // パラメータ取得By{id}部分
34+
operation.Parameters.Clear();
35+
operation.Parameters.Add(param);
36+
operation.Parameters.Add(new NonBodyParameter
37+
{
38+
Name = "uploadedFile",
39+
In = "formData",
40+
Description = "Upload File",
41+
Required = true,
42+
Type = "file"
43+
});
44+
operation.Consumes.Add("multipart/form-data");
45+
}
2946
}
3047

3148
}

WebApiFileUploadSample/src/WebApiFileUploadSample/Models/SQLServerDbContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
3434

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

37+
public DbSet<File> Files { get; set; }
38+
3739
protected override void OnModelCreating(ModelBuilder builder)
3840
{
3941
//builder.Entity<Value>().ToTable("Values");
4042
builder.Entity<Value>().HasKey(m => m.Id); // この場合、自動採番がデフォルトで、ID無しで登録になる。
43+
builder.Entity<File>().HasKey(m => m.Id); // この場合、自動採番がデフォルトで、ID無しで登録になる。
4144
base.OnModelCreating(builder);
4245
}
4346
}

WebApiFileUploadSample/src/WebApiFileUploadSample/Models/SQLiteDbContext.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
3636

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

39+
public DbSet<File> Files { get; set; }
40+
3941
protected override void OnModelCreating(ModelBuilder builder)
4042
{
4143
builder.Entity<Value>().HasKey(m => m.Id);
44+
builder.Entity<File>().HasKey(m => m.Id); // この場合、自動採番がデフォルトで、ID無しで登録になる。
45+
4246
base.OnModelCreating(builder);
4347
}
4448
}

WebApiFileUploadSample/src/WebApiFileUploadSample/Models/Value.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ public class Value
1111
public string Name { get; set; }
1212
public byte[] Image { get; set; }
1313
}
14+
public class File
15+
{
16+
public int Id { get; set; }
17+
public string Name { get; set; }
18+
public string Text { get; set; }
19+
}
1420
}

WebApiFileUploadSample/src/WebApiFileUploadSample/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public void ConfigureServices(IServiceCollection services)
4949
options.SingleApiVersion(new Info
5050
{
5151
Version = "v1",
52-
Title = "Geo Search API",
53-
Description = "A simple api to search using geo location in Elasticsearch",
52+
Title = "File Store API",
53+
Description = "A simple api to File Access",
5454
TermsOfService = "None"
5555
});
5656
//options.IncludeXmlComments(pathToDoc);
4 KB
Binary file not shown.

0 commit comments

Comments
 (0)