)
on IServiceCollection requires an explicit opt-in inside Startup.cs page. Please find the below documentation link:
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
namespace BlazorApp1.Data
{
[Route("api/[controller]")]
public class SampleDataController : Controller
{
private IHostingEnvironment hostingEnv;
public IActionResult Index()
{
return View();
}
public SampleDataController(IHostingEnvironment env)
{
this.hostingEnv = env;
}
[HttpPost("[action]")]
public async void Save(IList<IFormFile> UploadFiles)
{
try
{
foreach (var file in UploadFiles)
{
const string accountName = "exampleaccount";
const string key = "examplekey";
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, key), true);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("images");
await container.CreateIfNotExistsAsync();
await container.SetPermissionsAsync(new BlobContainerPermissions()
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
var blob = container.GetBlockBlobReference("test.jpg");
using (var stream = file.OpenReadStream())
{
await blob.UploadFromStreamAsync(stream);
}
}
}
catch (Exception e)
{
Response.Clear();
Response.StatusCode = 204;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
}
}
}
} |
Thanks for your update.
We have validated your reported scenario for large file
upload to Azure storage. By default, Azure file storage not merge the file to
same location and multiple chunk file stream. We suggest to use session for
save the chunk file. Once complete all chunk file, you can get the
session based on session key and save to Azure location. We have achieved your
scenario for save the chunk file to Azure location. Please find the code
snippet and sample for your reference.
Save.cs
[HttpPost]
[Route("Save")] public async Task
Save(IList<IFormFile> chunkFile)
{
try
{
foreach (var file in chunkFile)
{
var httpPostedChunkFile
= HttpContext.Request.Form.Files["chunkFile"]; var chunkIndex =
HttpContext.Request.Form["chunk-index"];
var totalChunk =
HttpContext.Request.Form["total-chunk"];
using (var fileStream =
file.OpenReadStream())
{
if(Convert.ToInt32(chunkIndex)
<= Convert.ToInt32(totalChunk))
{
var streamReader = new MemoryStream();
fileStream.CopyTo(streamReader);
var byteArr =
streamReader.ToArray();
var content = new byte[] { };
if
(HttpContext.Session.Get("streamFile") != null)
{
content = HttpContext.Session.Get("streamFile").Concat(byteArr).ToArray();
} else
{
content = byteArr;
}
HttpContext.Session.Set("streamFile", content); }
if(Convert.ToInt32(chunkIndex)
== Convert.ToInt32(totalChunk)-1)
{
var fileArray =
HttpContext.Session.Get("streamFile");
var storageCredentials
= new StorageCredentials("<AccName>", "<AccKey>");
var cloudStorageAccount
= new
CloudStorageAccount(storageCredentials, true);
var cloudBlobClient =
cloudStorageAccount.CreateCloudBlobClient();
var container =
cloudBlobClient.GetContainerReference("filo");
CloudBlockBlob blockBlob =
container.GetBlockBlobReference(httpPostedChunkFile.FileName);
using (FileStream
fileStreams = new FileStream(httpPostedChunkFile.FileName, FileMode.Create))
{
for (int i = 0; i <
fileArray.Length; i++)
{
fileStreams.WriteByte(fileArray[i]);
}
fileStreams.Seek(0, SeekOrigin.Begin);
HttpContext.Session.Remove("streamFile");
await
blockBlob.UploadFromStreamAsync(fileStreams);
}
} }
}
}
catch (Exception e)
{
. . . } } |
Startup.cs
public void ConfigureServices(IServiceCollection
services)
{
. . . services.AddSession(options
=> {
options.IdleTimeout = TimeSpan.FromMinutes(30);
}); . . . } public void Configure(IApplicationBuilder app,
IWebHostEnvironment env) { . . . app.UseSession(); . . . }
|
Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/BlazorApp1-1448603423
Regards,
Jeyanth.