mirror of https://github.com/abpframework/abp.git
17 changed files with 107 additions and 95 deletions
@ -0,0 +1,15 @@ |
|||
using System.IO; |
|||
|
|||
namespace Microsoft.AspNetCore.Http |
|||
{ |
|||
public static class AbpFormFileExtensions |
|||
{ |
|||
public static byte[] GetAllBytes(this IFormFile file) |
|||
{ |
|||
using (var stream = file.OpenReadStream()) |
|||
{ |
|||
return stream.GetAllBytes(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Blogging.Files |
|||
{ |
|||
public class RawFileDto |
|||
{ |
|||
public byte[] Bytes { get; set; } |
|||
} |
|||
} |
|||
@ -1,55 +0,0 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Volo.Abp; |
|||
using Volo.Abp.AspNetCore.Mvc; |
|||
using Volo.Blogging.Areas.Blog.Models; |
|||
using Volo.Blogging.Files; |
|||
using Volo.Blogging.Hosting; |
|||
|
|||
namespace Volo.Blogging.Areas.Blog.Controllers |
|||
{ |
|||
//TODO: This may be moved to HttpApi project since it may be needed by a SPA too.
|
|||
[Area("Blog")] |
|||
[Route("Blog/[controller]/[action]")]
|
|||
public class FilesController : AbpController |
|||
{ |
|||
private readonly IFileAppService _fileAppService; |
|||
|
|||
public FilesController(IFileAppService fileAppService) |
|||
{ |
|||
_fileAppService = fileAppService; |
|||
} |
|||
|
|||
[HttpPost] |
|||
public async Task<JsonResult> UploadImage(IFormFile file) |
|||
{ |
|||
//TODO: localize exception messages
|
|||
|
|||
if (file == null) |
|||
{ |
|||
throw new UserFriendlyException("No file found!"); |
|||
} |
|||
|
|||
if (file.Length <= 0) |
|||
{ |
|||
throw new UserFriendlyException("File is empty!"); |
|||
} |
|||
|
|||
if (!file.ContentType.Contains("image")) |
|||
{ |
|||
throw new UserFriendlyException("Not a valid image!"); |
|||
} |
|||
|
|||
var output = await _fileAppService.UploadAsync( |
|||
new FileUploadInputDto |
|||
{ |
|||
Bytes = file.AsBytes(), |
|||
Name = file.FileName |
|||
} |
|||
); |
|||
|
|||
return Json(new FileUploadResult(output.Url)); |
|||
} |
|||
} |
|||
} |
|||
@ -1,23 +0,0 @@ |
|||
using System.IO; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.Blogging.Hosting |
|||
{ |
|||
public static class FormFileExtensions |
|||
{ |
|||
public static byte[] AsBytes(this IFormFile file) //TODO: Move to the framework (rename to GetBytes)
|
|||
{ |
|||
using (var stream = file.OpenReadStream()) |
|||
{ |
|||
return stream.GetAllBytes(); |
|||
} |
|||
} |
|||
|
|||
public static void ValidateImage([CanBeNull] this IFormFile file) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
After Width: | Height: | Size: 129 KiB |
|
Before Width: | Height: | Size: 154 KiB After Width: | Height: | Size: 154 KiB |
|
Before Width: | Height: | Size: 400 KiB After Width: | Height: | Size: 400 KiB |
Loading…
Reference in new issue