Asp.net上传视频到YouTube

参考:http://www.cstruter.com/blog/313

/*
         1.下载Google Data API(http://code.google.com/p/google-gdata/downloads/list)
         2.获得Developer key(https://code.google.com/apis/youtube/dashboard)
         
         3.上傳后Video的路徑(http://www.youtube.com/embed/<%=Id%>)
         */
        string pDeveloperKey = "************************************";
        //YouTube的帳號和密碼(可以用googel帳號)
        string pUserName = "****************";
        string pPassword = "**********";
        private void btnUpload_Click(object sender, EventArgs e)
        {
            Upload();
        }
        /// <summary>
        /// 上傳
        /// </summary>
        private void Upload() {

            YouTubeRequestSettings settings = new YouTubeRequestSettings("WFAppUpload", pDeveloperKey, pUserName, pPassword);
            YouTubeRequest request = new YouTubeRequest(settings);
            Video newVideo = new Video();
            //必須提供這4個屬性(Title,Description,Category,Keywords)
            newVideo.Title = "Wildlife";
            //至少指定一個分類
            newVideo.Tags.Add(new MediaCategory("Animals", YouTubeNameTable.CategorySchema));
            newVideo.Keywords = "野生生物";
            newVideo.Description = "House,马";

            newVideo.YouTubeEntry.Private = false;
            //檔上傳文件很大時,設置timeout
           // ((GDataRequestFactory)request.Service.RequestFactory).Timeout = 9999999;
            newVideo.YouTubeEntry.MediaSource = new MediaFileSource("D:\\flv\\test.flv", "application/octet-stream");
            Video createdVideo = request.Upload(newVideo);
            //檔文檔上傳成功后,或返回一個Id和Status(200時表示上傳成功吧!!!)
            this.txtID.Text = createdVideo.Id;//tag:youtube.com,2008:video:RmVU96iTCEg
            
            //Google.GData.YouTube.State for: http://gdata.youtube.com/schemas/2007- state for: http://gdata.youtube.com/schemas/2007 - state
            this.txtName.Text = createdVideo.Status.ToString();
        
        }
        /// <summary>
        /// 獲得所有上傳的檔案
        /// </summary>
        /// <returns></returns>
        private IEnumerable<Video> ListMyVideos() 
        {
            YouTubeRequestSettings settings = new YouTubeRequestSettings("WFAppUpload", pDeveloperKey, pUserName, pPassword);
            YouTubeRequest request = new YouTubeRequest(settings);
            YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultUploads);
            Feed<Video> feed = request.Get<Video>(query);
            return feed != null ? feed.Entries : null;
        }
        
        /// <summary>
        /// 通過YouTube的用戶名和id獲得Video
        /// </summary>
        /// <param name="uploader">用戶名,不是帳號哦!</param>
        /// <param name="videoID"></param>
        /// <returns></returns>
        private Video GetMyVideo(string uploader, string videoID)
        {
            YouTubeRequestSettings settings = new YouTubeRequestSettings("WFAppUpload", pDeveloperKey, pUserName, pPassword);
            YouTubeRequest request = new YouTubeRequest(settings);
            Uri uri = new Uri(String.Format("http://gdata.YouTube.com/feeds/api/users/{0}/uploads/{1}", uploader, videoID));
            return request.Retrieve<Video>(uri);
        }
        /// <summary>
        /// 移除
        /// </summary>
        private void Remove(Video video)
        {
            YouTubeRequestSettings settings = new YouTubeRequestSettings("WFAppUpload", pDeveloperKey, pUserName, pPassword);
            YouTubeRequest request = new YouTubeRequest(settings);
            request.Delete(video);
        }
        
       /// <summary>
       /// 修改
       /// </summary>
        private void Update(Video video, string title, string description)
        {
            YouTubeRequestSettings settings = new YouTubeRequestSettings("WFAppUpload", pDeveloperKey, pUserName, pPassword);
            YouTubeRequest request = new YouTubeRequest(settings);
            video.Title = title;
            video.Description = description;
            request.Update(video);
        }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,您需要在ASP.NET Core项目中添加Microsoft.AspNetCore.Http和Microsoft.AspNetCore.StaticFiles包。然后在您的控制器中添加以下代码: ```csharp [HttpPost] [Route("api/uploadvideo")] public async Task<IActionResult> UploadVideo(IFormFile file) { if (file == null || file.Length == 0) { return BadRequest("File not selected."); } // Set the path where the video will be stored var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "videos", file.FileName); // Create the file in the specified path using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } return Ok("Video uploaded successfully."); } ``` 在这个示例中,我们使用`HttpPost`属性来指定这是一个POST请求,并使用`Route`属性来设置API的路由。我们还使用`IFormFile`类型的参数来接收上传视频文件。 在方法中,我们首先检查文件是否存在,如果不存在,则返回`BadRequest`结果。如果文件存在,则使用`Path.Combine`方法创建文件的保存路径。接着,我们使用`FileStream`类创建文件流,并使用`CopyToAsync`方法从上传文件流中复制数据到指定的文件流中。最后,我们使用`Ok`方法返回上传成功的消息。请注意,我们将视频文件存储在`wwwroot`文件夹中,这是ASP.NET Core应用程序的默认静态文件目录。 当用户使用POST请求上传视频时,他们可以使用类似于以下示例的代码: ```html <form method="post" enctype="multipart/form-data" action="/api/uploadvideo"> <input type="file" name="file" /> <button type="submit">Upload Video</button> </form> ``` 在这个示例中,我们使用`enctype="multipart/form-data"`属性来指定这是一个多部分表单,以便能够上传文件。我们还使用`input`标签的`type="file"`属性来允许用户选择视频文件。最后,我们使用`action`属性来指定上传视频的API路由。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值