Hi Gich,
Thanks for your patience.
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 { 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("
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/forum/150303/ze/svc598742026
Regards,
Gopi G.
[On Multiple Request File Some time upload half on azure]. Can you please provide me solution?
public async Task UploadFile(IList
{
try
{
IFormFile chunkFile = Request.Form.Files[0];
if (chunkFile.Length > 0)
{
var ProductName = HttpContext.Request.Form["ProductName"];
var ProductReleaseNo = HttpContext.Request.Form["ProductReleaseNo"];
var filehash = HttpContext.Request.Form["fileHash"];
var chunkIndex = HttpContext.Request.Form["chunk-index"];
var totalChunk = HttpContext.Request.Form["total-chunk"];
bool SupportFile = Convert.ToBoolean(HttpContext.Request.Form["SupportFile"]);
if (ProductName != "" && ProductReleaseNo != "")
{
using (var fileStream = chunkFile.OpenReadStream())
{
if (Convert.ToInt32(chunkIndex) <= Convert.ToInt32(totalChunk))
{
var streamReader = new MemoryStream();
fileStream.CopyTo(streamReader);
var byteArr = streamReader.ToArray();
if (content.Length > 0)
{
content = content.Concat(byteArr).ToArray();
}
else
{
content = byteArr;
}
}
if (Convert.ToInt32(chunkIndex) == Convert.ToInt32(totalChunk) - 1 || Convert.ToInt32(chunkIndex) == Convert.ToInt32(totalChunk))
{
String strorageconn = Sensit.Activation.API.Helpers.Constants.AzureConnectionString;
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(strorageconn);
var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = cloudBlobClient.GetContainerReference("clickonce-app-container");
CloudBlobDirectory folder = container.GetDirectoryReference(ProductName);
await container.CreateIfNotExistsAsync();
CloudBlobDirectory ReleaseFolder = folder.GetDirectoryReference(ProductReleaseNo);
await container.CreateIfNotExistsAsync();
CloudBlockBlob blockBlob;
blockBlob = ReleaseFolder.GetBlockBlobReference(chunkFile.FileName);
await container.CreateIfNotExistsAsync();
bytes = GetHashSha256(content);
var UploadFileSHA256 = BitConverter.ToString(bytes).Replace("-", string.Empty).ToLower();
//bool compare = CompareHashKey(UploadFileSHA256, filehash);
//if (compare == false)
//{
// Response.Clear();
// Response.StatusCode = 419;
//}
//else
//{
using (MemoryStream ms = new MemoryStream(content))
{
await blockBlob.UploadFromStreamAsync(ms);
ms.Flush();
content = new byte[] { };
}
Response.Clear();
Response.StatusCode = 200;
//}
}
#region Commment Code [This code Create file in this project then Upload on azure]
//using (FileStream fileStreams = new FileStream(chunkFile.FileName, FileMode.Create))
//{
// for (int i = 0; i < content.Length; i++)
// {
// fileStreams.WriteByte(content[i]);
// }
// fileStreams.Seek(0, SeekOrigin.Begin);
// //Get All Bytes Hash generated
// bytes = GetHashSha256(content);
// var UploadFileSHA256 = BitConverter.ToString(bytes).Replace("-", string.Empty).ToLower();
// bool compare = CompareHashKey(UploadFileSHA256, filehash);
// if (compare == false)
// {
// Response.Clear();
// Response.StatusCode = 419;
// }
// else
// {
// content = new byte[] { };
// await blockBlob.UploadFromStreamAsync(fileStream);
// Response.Clear();
// Response.StatusCode = 200;
// }
//}
#endregion
}
}
else
{
Response.Clear();
Response.StatusCode = 404;
}
}
else
{
Response.Clear();
Response.StatusCode = 404;
}
}
catch (Exception e)
{
content = new byte[] { };
Response.Clear();
Response.StatusCode = 400;
}
}