Skip to content

Instantly share code, notes, and snippets.

@timReynolds
Created September 20, 2018 08:48
Show Gist options
  • Save timReynolds/c380400f97f1c4298c4514d118c39975 to your computer and use it in GitHub Desktop.
Save timReynolds/c380400f97f1c4298c4514d118c39975 to your computer and use it in GitHub Desktop.
IFormFile to Stream example
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);
}
}
}
}
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