如何使用api进行多部分视频上传

时间:2013-02-12 10:23:12

标签: asp.net api upload httprequest

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

我使用以下代码创建项目并使用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

如何上传

1 个答案:

答案 0 :(得分:3)

我无法测试它(因为我没有帐户),但一般代码应如下所示:

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();

apiPasswordprojectIdcontactIdname是API文档中描述的参数,而MyPath\MyWistiaFile.ext是您要上传的文件的路径。