1

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

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;
            }
        }

    }

I am using Upload API from here: "http://wistia.com/doc/upload-api"

how to do uploading

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
donstack
  • 2,557
  • 3
  • 29
  • 44

1 Answers1

3

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

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.

tpeczek
  • 23,867
  • 3
  • 74
  • 77
  • It is giving Internal Server Error 500 – donstack Feb 12 '13 at 11:29
  • @user1750877 Is it comming from your code or is it the response from the remote server? In both cases there should some additional infor either in InnerException or in response body. – tpeczek Feb 12 '13 at 12:21
  • 1
    @user1750877 There was also a small bug in one of `Content-Disposition` (missing file name), please check update answer. – tpeczek Feb 12 '13 at 12:25
  • But, it is now working for large files. I am trying to upload 213 MB file and code is throwing exception 'response is not available in this context' – donstack Mar 11 '13 at 12:34
  • @user1750877 Hard to say exactly without any repro project. It might be specific to either somehting n your code or something on thew other side. – tpeczek Mar 11 '13 at 14:13
  • @tpeczek I tried this solution it worked for a small mp4 file but for 100mb file its throws exception at --requestStream.Write(postData, 0, postData.Length); --(The request was aborted.) Can you help me out here as I need to upload up to 1gb files – Scorpio Apr 19 '17 at 14:49
  • 1
    @Scorpio for such a big files trying to cache it in temporary stream will be both memory and time consuming. For such cases you should probably write directly to the response by asynchronously reading the file in chunks, and asynchronously writting those chunks to the request stream (maybe in a loop using callbacks). You may also want to manage buffer size. – tpeczek Apr 19 '17 at 15:24
  • @tpeczek sorry to bother you again. Conceptually I understood wat you are trying to say but I am not able to figure out what is the scope of this change considering the above code..I have a question posted can you briefly tell me wat section of code needs to be changed... – Scorpio Apr 19 '17 at 15:38
  • @Scorpio I will take a look at this, but tomorrow as I'm going to be offline for the rest of the day. I hope this can wait (or someone else will answer sooner). – tpeczek Apr 19 '17 at 16:38