Note that there are some explanatory texts on larger screens.

plurals
  1. POUploading videos with YouTube API and CORS
    primarykey
    data
    text
    <p>I'm trying to allow visitors to our site to upload videos to our YouTube channel. As a result, I don't want to handle authentication with OAuth otherwise I'm going to need to hand out our username and password everywhere. And I don't want direct uploading as I don't want to host the videos myself.</p> <p>So, I have a form that collects some details in a panel, the button forces a post back to the code behind where I create the Video object and get the FormUploadToken. I then use the information to create a that is posted into a Literal on the page. </p> <p>The file is chosen and then the upload button is clicked. Immediately, the abort event is triggered but there's no reason as to why. </p> <p>Any ideas?? Help would be much appreciated:</p> <pre><code> &lt;form id="Form1" runat="server"&gt; &lt;asp:Literal ID="Literal1" runat="server"&gt;&lt;/asp:Literal&gt; &lt;br /&gt; &lt;dx:ASPxPanel ID="ASPxPanel1" runat="server" Width="200px"&gt; &lt;PanelCollection&gt; &lt;dx:PanelContent ID="PanelContent1" runat="server" SupportsDisabledAttribute="True"&gt; &lt;dx:ASPxLabel ID="ASPxLabel1" runat="server" Text="Name"&gt; &lt;/dx:ASPxLabel&gt; &lt;dx:ASPxTextBox ID="NameTextBox" runat="server" Width="170px" Text="Simon"&gt; &lt;/dx:ASPxTextBox&gt; &lt;dx:ASPxLabel ID="ASPxLabel2" runat="server" Text="Email address"&gt; &lt;/dx:ASPxLabel&gt; &lt;dx:ASPxTextBox ID="EmailTextBox" runat="server" Width="170px" Text="info@example.com"&gt; &lt;/dx:ASPxTextBox&gt; &lt;dx:ASPxLabel ID="ASPxLabel3" runat="server" Text="Video title"&gt; &lt;/dx:ASPxLabel&gt; &lt;dx:ASPxTextBox ID="VideoTitleTextBox" runat="server" Width="170px" Text="My new video title"&gt; &lt;/dx:ASPxTextBox&gt; &lt;dx:ASPxLabel ID="ASPxLabel4" runat="server" Text="Keywords - comma separated"&gt; &lt;/dx:ASPxLabel&gt; &lt;dx:ASPxTextBox ID="KeywordsTextBox" runat="server" Width="170px" Text="India, Kochin"&gt; &lt;/dx:ASPxTextBox&gt; &lt;dx:ASPxLabel ID="ASPxLabel5" runat="server" Text="Video description"&gt; &lt;/dx:ASPxLabel&gt; &lt;dx:ASPxTextBox ID="VideoDescriptionTextBox" runat="server" Width="170px" Text="An amazing description that makes a lot of sense"&gt; &lt;/dx:ASPxTextBox&gt; &lt;dx:ASPxButton ID="ASPxButton1" runat="server" onclick="VideoUploadBttn_Click" Text="ASPxButton"&gt; &lt;/dx:ASPxButton&gt; &lt;/dx:PanelContent&gt; &lt;/PanelCollection&gt; &lt;/dx:ASPxPanel&gt; &lt;/form&gt; &lt;dx:ASPxPanel ID="ASPxPanel2" runat="server" Width="200px" Visible="False"&gt; &lt;PanelCollection&gt; &lt;dx:PanelContent ID="PanelContent2" runat="server" SupportsDisabledAttribute="True"&gt; &lt;asp:Literal ID="Literal2" runat="server"&gt;&lt;/asp:Literal&gt; &lt;/dx:PanelContent&gt; &lt;/PanelCollection&gt; &lt;/dx:ASPxPanel&gt; &lt;script type="text/javascript"&gt; function fileSelected() { var file = document.getElementById('fileToUpload').files[0]; if (file) { var fileSize = 0; if (file.size &gt; 1024 * 1024) fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB'; else fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB'; document.getElementById('fileName').innerHTML = 'Name: ' + file.name; document.getElementById('fileSize').innerHTML = 'Size: ' + fileSize; document.getElementById('fileType').innerHTML = 'Type: ' + file.type; } } function uploadFile() { var fd = new FormData(); fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]); var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", uploadProgress, false); xhr.addEventListener("load", uploadComplete, false); xhr.addEventListener("error", uploadFailed, false); xhr.addEventListener("abort", uploadCanceled, false); xhr.open("POST", document.UploadForm.action); console.log('action url: ' + document.UploadForm.action); xhr.send(fd); } function uploadProgress(evt) { if (evt.lengthComputable) { var percentComplete = Math.round(evt.loaded * 100 / evt.total); document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%'; } else { document.getElementById('progressNumber').innerHTML = 'unable to compute'; } } function uploadComplete(evt) { /* This event is raised when the server send back a response */ alert(evt.target.responseText); } function uploadFailed(evt) { alert("There was an error attempting to upload the file."); } function uploadCanceled(evt) { alert("The upload has been canceled by the user or the browser dropped the connection."); console.log(evt); } &lt;/script&gt; </code></pre> <p>And then the code behind looks like this:</p> <pre><code>protected void VideoUploadBttn_Click(object sender, EventArgs e) { Video newVideo = new Video(); newVideo.Title = VideoTitleTextBox.Text; newVideo.Tags.Add(new MediaCategory("Travel", YouTubeNameTable.CategorySchema)); newVideo.Keywords = KeywordsTextBox.Text; newVideo.Description = VideoDescriptionTextBox.Text; newVideo.YouTubeEntry.Private = false; newVideo.Keywords = "Travel"; newVideo.Tags.Add(new MediaCategory("Motorbike", YouTubeNameTable.DeveloperTagSchema)); // alternatively, you could just specify a descriptive string // newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA"); YouTubeRequestSettings settings = new YouTubeRequestSettings(_AppName, _APIKey, _Username, _Password); YouTubeRequest request = new YouTubeRequest(settings); FormUploadToken uploadToken = request.CreateFormUploadToken(newVideo); string actionUrl = uploadToken.Url; string token = uploadToken.Token; StringBuilder sb = new StringBuilder(); string url = String.Format("{0}?nexturl={1}", actionUrl, System.Web.HttpUtility.UrlEncode("http://bbc.co.uk")); sb.AppendFormat("&lt;form name=\"UploadForm\" action=\"{0}\" method =\"post\" enctype=\"multipart/form-data\" &gt;", url); sb.Append("&lt;input type=\"file\" name=\"file\" id=\"fileToUpload\" onchange=\"fileSelected();\" /&gt;"); sb.AppendFormat("&lt;input type=\"hidden\" name=\"token\" value=\"{0}\"/&gt;", token); sb.Append("&lt;div id=\"fileName\"&gt;&lt;/div&gt;"); sb.Append("&lt;div id=\"fileSize\"&gt;&lt;/div&gt;"); sb.Append("&lt;div id=\"fileType\"&gt;&lt;/div&gt;"); sb.Append("&lt;input type=\"submit\" value=\"Upload\" onclick=\"uploadFile()\" /&gt;"); sb.Append("&lt;div id=\"progressNumber\"&gt;&lt;/div&gt;"); sb.Append("&lt;/form&gt;"); Literal2.Text = sb.ToString(); ASPxPanel1.Visible = false; ASPxPanel2.Visible = true; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload