Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vibhavsinha/cb35c43b874e52c69dd2 to your computer and use it in GitHub Desktop.
Save vibhavsinha/cb35c43b874e52c69dd2 to your computer and use it in GitHub Desktop.
Sample code for browser video upload from ASP.NET with C#
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Upload()
{
ViewBag.Message = "Upload new videos here";
string new_video_title = "VIDEO_TITLE"; // This should be obtained from DB
string api_secret = System.Web.Configuration.WebConfigurationManager.AppSettings["VdoCipher_API_Key"]; ;
string uri = "https://api.vdocipher.com/v2/uploadPolicy/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
writer.Write("clientSecretKey=" + api_secret + "&title=" + new_video_title);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
dynamic otp_data;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string json_otp = reader.ReadToEnd();
otp_data = JObject.Parse(json_otp);
}
ViewBag.upload_data = otp_data;
return View();
}
}
}
@{
ViewBag.Title = "Upload";
}
<h2>@ViewBag.Title.</h2>
<p>Upload your videos using this form.</p>
<form action="@ViewBag.upload_data.upload_link_secure" method="post" enctype="multipart/form-data">
<p>The following text fields are showm for illustrative purposes. Keep these hidden.</p>
<input type="text" name="key" value="@ViewBag.upload_data.key" /><br />
<input type="text" name="X-Amz-Credential" value="@ViewBag.upload_data["x-amz-credential"]" /><br />
<input type="text" name="X-Amz-Algorithm" value="@ViewBag.upload_data["x-amz-algorithm"]" /><br />
<input type="text" name="X-Amz-Date" value="@ViewBag.upload_data["x-amz-date"]" /><br />
<input type="text" name="Policy" value='@ViewBag.upload_data.policy' /><br />
<input type="text" name="X-Amz-Signature" value="@ViewBag.upload_data["x-amz-signature"]" /><br />
<input type="text" name="success_action_status" value="201" />
<input type="text" name="success_action_redirect" value="https://example.com/" />
File:
<input type="file" name="file" accept="video/*" /> <br />
<!-- The elements after this will be ignored -->
<input type="submit" name="submit" value="Upload to Amazon S3" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment