如何使用API​​做多部分的视频上传 [英] how to do multi-part video upload using api

查看:172
本文介绍了如何使用API​​做多部分的视频上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是多部分的视频上传以及它是如何在asp.net中完成的。

What is multi-part video upload and how it is done in asp.net.

我用低于code创建项目,并使用API​​获取列表

I have used below code to create project and get list using API

    HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create("apiurl?paramerters");
    wRequest.Method = "POST";

    string credentials = String.Format("{0}:{1}", "username", "password");
    byte[] bytes = Encoding.ASCII.GetBytes(credentials);
    string base64 = Convert.ToBase64String(bytes);
    string authorization = String.Concat("Basic ", base64);


    wRequest.Headers.Add("Authorization", authorization);

    wResponse = (HttpWebResponse)wRequest.GetResponse();


    if (wRequest.HaveResponse)
    {
        string response = (new StreamReader(wResponse.GetResponseStream())).ReadToEnd();
        if (wResponse.StatusCode == HttpStatusCode.OK)
        {
            dsResponse.ReadXml(new MemoryStream(Encoding.UTF8.GetBytes(WistiaResponse)));

            if (dsResponse.Tables.Count > 0)
            {
                dtResponse = dsResponse.Tables[0];
            }
            else
            {
                dtResponse = null;
            }
        }

    }

我使用的上传API从这里开始: http://wistia.com/doc/upload-api

怎么办上传

推荐答案

我无法测试它(因为我没有一个帐户存在),但一般code应该是这样的:

I can't test it (as I don't have an account there), but general code should look like this:

string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); 
StringBuilder postDataBuilder = new StringBuilder(); 
postDataBuilder.Append("--" + boundary + "\r\n"); 
postDataBuilder.Append("Content-Disposition: form-data; name=\"api_password\"\r\n\r\n"); 
postDataBuilder.Append(apiPassword); 
postDataBuilder.Append("\r\n--" + boundary + "\r\n"); 
postDataBuilder.Append("Content-Disposition: form-data; name=\"project_id\"\r\n\r\n"); 
postDataBuilder.Append(projectId); 
postDataBuilder.Append("\r\n--" + boundary + "\r\n");
postDataBuilder.Append("Content-Disposition: form-data; name=\"contact_id\"\r\n\r\n"); 
postDataBuilder.Append(contactId); 
postDataBuilder.Append("\r\n--" + boundary + "\r\n"); 
postDataBuilder.Append("Content-Disposition: form-data; name=\"name\"\r\n\r\n"); 
postDataBuilder.Append(name); 
postDataBuilder.Append("\r\n--" + boundary + "\r\n"); 
postDataBuilder.AppendFormat("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\nContent-Type: application/octet-stream\r\n\r\n", "MyWistiaFile.ext"); 


byte[] postData = null; 
using (MemoryStream postDataStream = new MemoryStream()) 
{ 
        byte[] postDataBuffer = Encoding.UTF8.GetBytes(postDataBuilder.ToString()); 
        postDataStream.Write(postDataBuffer, 0, postDataBuffer.Length); 
        using (FileStream wistiaFileStream = new FileStream("MyPath\\MyWistiaFile.ext", FileMode.Open, FileAccess.Read)) 
        { 
                byte[] wistiaFileBuffer = new byte[1024]; 
                int wistiaFileBytesRead = 0; 
                while ((wistiaFileBytesRead = wistiaFileStream.Read(wistiaoFileBuffer, 0, wistiaFileBuffer.Length)) != 0) 
                        postDataStream.Write(wistiaFileBuffer, 0, wistiaFileBytesRead); 
        } 
        postDataBuffer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--"); 
        postDataStream.Write(postDataBuffer, 0, postDataBuffer.Length); 
        postData = postDataStream.ToArray(); 
} 

System.Net.ServicePointManager.Expect100Continue = false; 
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create("https://upload.wistia.com"); 
request.Method = "POST"; 
request.Expect = String.Empty; 
request.Headers.Clear(); 
request.ContentType = "multipart/form-data; boundary=" + boundary; 
request.ContentLength = postData.Length; 


Stream requestStream = request.GetRequestStream(); 
requestStream.Write(postData, 0, postData.Length); 
requestStream.Flush(); 
requestStream.Close(); 


HttpWebResponse response = (HttpWebResponse)request.GetResponse();

其中, apiPassword 专案编号的ContactID 名称是参数API文档中描述,而 mypath中\\ MyWistiaFile.ext 是路径要上传的文件。

Where apiPassword, projectId, contactId and name are parameters described in API documentation, while MyPath\MyWistiaFile.ext is path to the file you want to upload.

这篇关于如何使用API​​做多部分的视频上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆