Search code examples
asp.netgoogle-apivisual-web-developer-2010youtube.net-api

Error for uploading video to youtube server through google api


This my code

upload video size 7Mb

upload video type mp4

category.SelectedItem.Text : video/mp4

if (FileUpload1.PostedFile != null)
{

        HttpPostedFile postfiles = FileUpload1.PostedFile;
        if (postfiles.ContentLength > 0)
        {
            System.IO.Stream iStream = postfiles.InputStream;
            byte[] filedata = new byte[postfiles.ContentLength];
            iStream.Read(filedata, 0, postfiles.ContentLength);
            postfiles.SaveAs(@"c:\Temp\" + postfiles.FileName);
            YouTubeRequestSettings settings = new YouTubeRequestSettings("AAA", youtube_DVkey, g_username, g_password);
            YouTubeRequest request = new YouTubeRequest(settings);
            Video newvideo = new Video();
            newvideo.Title = txtTitle.Text;
            newvideo.Tags.Add(new MediaCategory(category.SelectedItem.Text, YouTubeNameTable.CategorySchema));
            newvideo.Keywords = txtKeywords.Text;
            newvideo.Description = txtDescription.Text;
            newvideo.Private = false;
            newvideo.Tags.Add(new MediaCategory(txtTitle.Text.Trim(), YouTubeNameTable.DeveloperTagSchema));
            newvideo.YouTubeEntry.Location = new Google.GData.Extensions.Location.GeoRssWhere(37, -122);
            newvideo.YouTubeEntry.MediaSource = new MediaFileSource(@"c:\Temp\" + postfiles.FileName, Video_type.SelectedItem.Text);
            try
            {
                Video result_video = request.Upload(youtubeaccount, newvideo);
                File.Delete(@"c:\Temp\" + postfiles.FileName);
                Response.Redirect("gallery.aspx");

            }
            catch (Google.GData.Client.GDataRequestException ex)
            {
                Response.Write(ex.ToString());
            }
            catch (Google.GData.Client.InvalidCredentialsException ex)
            {
                Response.Write("Invalid credentials" + ex.ToString());
            }
            catch(Exception ex)
            {
                Response.Write("Failed to upload!" + ex);
            }
        }
    }

Exception

Failed to upload!System.Net.WebException: The request was aborted: The request was canceled. ---> System.IO.IOException: Cannot close stream until all bytes are written. at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting) --- End of inner exception stack trace --- at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting) at System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState) at System.Net.ConnectStream.Dispose(Boolean disposing) at System.IO.Stream.Close() at Google.GData.Client.GDataGAuthRequest.CopyRequestData() at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter) at Google.GData.Client.GDataGAuthRequest.Execute() at Google.GData.Client.MediaService.EntrySend(Uri feedUri, AtomBase baseEntry, GDataRequestType type, AsyncSendData data) at Google.GData.Client.Service.Insert(Uri feedUri, AtomEntry newEntry, AsyncSendData data) at Google.GData.Client.Service.Insert[TEntry](Uri feedUri, TEntry entry) at Google.GData.YouTube.YouTubeService.Upload(String userName, YouTubeEntry entry) at Google.YouTube.YouTubeRequest.Upload(String userName, Video v) at Youtube_upload.Page_Load(Object sender, EventArgs e) in d:\Kidzaina_Final\video_upload.aspx.cs:line 67


Solution

  • You have to use the resumable upload mechanism to upload big files. Check the YouTubeUploader source for a complete sample using the ResumableUploader component:

    http://code.google.com/p/google-gdata/source/browse/#svn%2Ftrunk%2Fclients%2Fcs%2Fsamples%2FYouTubeUploader