Skip to content

Commit

Permalink
Resumable upload sample added
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldwight committed May 12, 2023
1 parent 9a71bc9 commit ee20c08
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 22 deletions.
77 changes: 57 additions & 20 deletions Samples/WhatsAppBusinessCloudAPI.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -798,45 +798,82 @@ public async Task<IActionResult> SendWhatsAppContactMessage(SendContactMessageVi

public IActionResult UploadMedia()
{
return View();
UploadMediaViewModel uploadMediaViewModel = new UploadMediaViewModel();
uploadMediaViewModel.UploadType = new List<SelectListItem>()
{
new SelectListItem(){ Text = "Normal Upload", Value = "Normal Upload" },
new SelectListItem(){ Text = "Resumable Upload", Value = "Resumable Upload" },
};

return View(uploadMediaViewModel);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UploadMedia(IFormFile mediaFile)
public async Task<IActionResult> UploadMedia(UploadMediaViewModel uploadMediaViewModel, IFormFile mediaFile)
{
try
{
var fileName = Path.GetFileName(mediaFile.FileName).Trim('"');
var fileName = Path.GetFileName(mediaFile.FileName).Trim('"');

var rootPath = Path.Combine(_environment.WebRootPath, "Application_Files\\MediaUploads\\");
var rootPath = Path.Combine(_environment.WebRootPath, "Application_Files\\MediaUploads\\");

if (!Directory.Exists(rootPath))
{
Directory.CreateDirectory(rootPath);
}

if (!Directory.Exists(rootPath))
// Get the path of filename
var filePath = Path.Combine(_environment.WebRootPath, "Application_Files\\MediaUploads\\", fileName);

// Upload Csv file to the browser
using (var stream = new FileStream(filePath, FileMode.Create))
{
await mediaFile.CopyToAsync(stream);
}

if (uploadMediaViewModel.SelectedUploadType.Equals("Normal Upload", StringComparison.OrdinalIgnoreCase))
{
Directory.CreateDirectory(rootPath);
}
UploadMediaRequest uploadMediaRequest = new UploadMediaRequest();
uploadMediaRequest.File = filePath;
uploadMediaRequest.Type = mediaFile.ContentType;

var uploadMediaResult = await _whatsAppBusinessClient.UploadMediaAsync(uploadMediaRequest);

// Get the path of filename
var filePath = Path.Combine(_environment.WebRootPath, "Application_Files\\MediaUploads\\", fileName);

// Upload Csv file to the browser
using (var stream = new FileStream(filePath, FileMode.Create))
ViewBag.MediaId = uploadMediaResult.MediaId;
}
else // Resumable upload generates header file response to be used for creating message templates
{
await mediaFile.CopyToAsync(stream);
}
var resumableUploadMediaResult = await _whatsAppBusinessClient.CreateResumableUploadSessionAsync(mediaFile.Length, mediaFile.ContentType, mediaFile.FileName);

if (resumableUploadMediaResult is not null)
{
var uploadSessionId = resumableUploadMediaResult.Id;

UploadMediaRequest uploadMediaRequest = new UploadMediaRequest();
uploadMediaRequest.File = filePath;
uploadMediaRequest.Type = mediaFile.ContentType;
var resumableUploadResponse = await _whatsAppBusinessClient.UploadFileDataAsync(uploadSessionId, filePath, mediaFile.ContentType);

var queryResumableUploadStatus = await _whatsAppBusinessClient.QueryFileUploadStatusAsync(uploadSessionId);

if (resumableUploadResponse is not null)
{
ViewBag.H = resumableUploadResponse.H;
}

var uploadMediaResult = await _whatsAppBusinessClient.UploadMediaAsync(uploadMediaRequest);
ViewBag.MediaId = uploadMediaResult.MediaId;
return View().WithSuccess("Success", "Successfully upload media.");
if (queryResumableUploadStatus is not null)
{
ViewBag.StatusId = queryResumableUploadStatus.Id;
ViewBag.FileOffset = queryResumableUploadStatus.FileOffset;
}
}
}

return View(uploadMediaViewModel).WithSuccess("Success", "Successfully upload media.");
}
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return View().WithDanger("Error", ex.Message);
return RedirectToAction(nameof(UploadMedia)).WithDanger("Error", ex.Message);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Mvc.Rendering;

namespace WhatsAppBusinessCloudAPI.Web.ViewModel
{
public class UploadMediaViewModel
{
public List<SelectListItem> UploadType { get; set; }
public string SelectedUploadType { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@{
@model UploadMediaViewModel
@{
ViewData["Title"] = "Upload Media to WhatsApp";
ViewData["CurrentPage"] = "Upload Media";
Layout = "~/Views/Shared/AdminLTE/_AdminLayout.cshtml";
Expand All @@ -24,11 +25,27 @@
<label class="custom-file-label">Choose Media File</label>
</div>
</div>
<div class="form-group">
<label class="control-label">Upload Type</label>
<select class="form-control form-select form-select-lg mb-3" asp-for="SelectedUploadType" asp-items="@Model.UploadType"></select>
<span asp-validation-for="SelectedUploadType" class="form-control" />
</div>

@if (ViewBag.MediaId != null)
{
<p>Media Id from WhatsApp: @ViewBag.MediaId</p>
}

@if (ViewBag.H != null)
{
<p>H from WhatsApp: @ViewBag.H</p>
}

@if (ViewBag.StatusId != null)
{
<p>File Upload Status Id: @ViewBag.StatusId</p>
<p>File Offset: @ViewBag.FileOffset</p>
}
</div> <!--/. card-body -->
<div class="card-footer">
<button type="submit" name="submit" class="btn btn-primary">Upload Media</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit ee20c08

Please sign in to comment.