-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c823c0a
commit 6c0a8e3
Showing
4 changed files
with
85 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace FileUploadSample | ||
{ | ||
public class UploadRepository | ||
{ | ||
private readonly ConcurrentBag<File> _files; | ||
private readonly string _uploadDirectory; | ||
|
||
public UploadRepository() | ||
{ | ||
_files = new ConcurrentBag<File>(); | ||
_uploadDirectory = Path.Combine(Directory.GetCurrentDirectory(), "uploads"); | ||
Directory.CreateDirectory(_uploadDirectory); | ||
} | ||
|
||
public async Task<File> Save(IFormFile formFile) | ||
{ | ||
var id = Guid.NewGuid().ToString().Substring(0, 8); | ||
var path = Path.Combine(_uploadDirectory, id + Path.GetExtension(formFile.FileName)); | ||
|
||
using (var fs = formFile.OpenReadStream()) | ||
using (var ws = System.IO.File.Create(path)) | ||
{ | ||
await fs.CopyToAsync(ws); | ||
} | ||
|
||
var file = new File | ||
{ | ||
Id = id, | ||
MimeType = formFile.ContentType, | ||
Name = formFile.FileName, | ||
Path = path | ||
}; | ||
_files.Add(file); | ||
|
||
return file; | ||
} | ||
|
||
public IEnumerable<File> Files => _files; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters