-
-
Save timReynolds/c380400f97f1c4298c4514d118c39975 to your computer and use it in GitHub Desktop.
IFormFile to Stream example
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
using System.IO; | |
namespace VestaProperty.Core.File | |
{ | |
public class File | |
{ | |
public File() | |
{ | |
this.Content = (Stream) new MemoryStream(); | |
} | |
public string Name { get; set; } | |
public Stream Content { get; set; } | |
public string ContentType { get; set; } | |
public long ContentLength { get; set; } | |
public string Extension | |
{ | |
get | |
{ | |
return Path.GetExtension(this.Name); | |
} | |
} | |
} | |
} |
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
using Microsoft.AspNetCore.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace VestaProperty.Core.File.FormFile | |
{ | |
public static class FormFileExtensions | |
{ | |
public static async Task<VestaProperty.Core.File.File> NewFile(this IFormFile formFile) | |
{ | |
VestaProperty.Core.File.File file = new VestaProperty.Core.File.File() | |
{ | |
ContentLength = formFile.Length, | |
ContentType = formFile.ContentType, | |
Name = formFile.FileName | |
}; | |
await formFile.CopyToAsync(file.Content, new CancellationToken()); | |
return file; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment