1

I've been trying to sort this out for weeks without any luck. I'm pretty sure i lack basic knowledge in memory management in .NET. I'm not sure where to start googling though.

This is the API i've been following: https://developer.vimeo.com/api/upload#start-streaming

If a user uploads a video which is under 100mb it works okey, but it takes forever to upload. However, if the file is more than 100mb, the connection times out because the server lacks memory. I think i need to upload the file in smaller parts.

This is my current uploadmethod:

public static string UploadVideo(HttpPostedFileBase file, dynamic ticket)
        {
            try
            {
                var apiUrl = ticket.upload_link_secure;
                byte[] fileData;
                using (var binaryReader = new BinaryReader(file.InputStream))
                {
                    fileData = binaryReader.ReadBytes(file.ContentLength);
                }



                using (var client = new WebClient())
                {
                    client.Headers.Add(HttpRequestHeader.Authorization, "bearer " + AccessToken);
                    client.Headers.Add("Accept", "application/vnd.vimeo.*+json;version=3.0");
                    client.UploadData(apiUrl, "PUT", fileData.ToArray());
                }



            }

            catch (Exception ex)
            {
                EventLog.LogEvent("Error uploading video: ", ex.Message, ex.ToString(),
                    EventTypes.Error, false, "");
            }
            return "Failed";



        }

Right now i'm uploading the whole thing into one byte array which probably is bad practise. Do i take the whole content and divide it into parts by, lets say 3mb and then upload it? Wouldnt that force me to call verifyUpload alot of times just to upload one video? Lets say the

sindrem
  • 1,211
  • 5
  • 24
  • 45
  • I don't really understand your use case, but the obvious flaw with your example is that you are converting the stream into a byte array, which will eat up the memory on your server. You need to stream the data all the way through without converting it into bytes by using `Stream.CopyTo`. I can't figure out why you are having your client post the file to a .NET site first and then re-posting it to another server - it seems redundant and unnecessary. – NightOwl888 Mar 08 '15 at 13:38
  • What would be the solution then? Using javascript to upload it? Doesnt vimeodotnet also post the file to .net first, then upload it to vimeo? – sindrem Mar 08 '15 at 14:20
  • But, do i need to save the file to disk before uploading it to vimeo? – sindrem Mar 08 '15 at 14:23

1 Answers1

0

Have you tried to just send it directly from stream?

Not sure id it helps but at least it's worth a try.

public static string UploadVideo(HttpPostedFileBase file, dynamic ticket)
    {
        try
        {
            var apiUrl = ticket.upload_link_secure;

            using (var client = new WebClient())
            {
                client.Headers.Add(HttpRequestHeader.Authorization, "bearer " + AccessToken);
                client.Headers.Add("Accept", "application/vnd.vimeo.*+json;version=3.0");
                using (var binaryReader = new BinaryReader(file.InputStream))
                {
                   client.UploadData(apiUrl, "PUT", binaryReader.ReadBytes(file.ContentLength));

                    //SAVE FILE TO DISK
                    //http://stackoverflow.com/questions/3914445/how-to-write-contents-of-one-file-to-another-file
                    using (FileStream writeStream = File.OpenWrite("D:\\file2.txt"))
                    {
                    BinaryWriter writer = new BinaryWriter(writeStream);

                    // create a buffer to hold the bytes 
                    byte[] buffer = new Byte[1024];
                    int bytesRead;

                    // while the read method returns bytes
                    // keep writing them to the output stream
                    while ((bytesRead =
                            binaryReader.Read(buffer, 0, 1024)) > 0)
                    {
                        writeStream.Write(buffer, 0, bytesRead);
                    }
                }
            }
        }

        catch (Exception ex)
        {
            EventLog.LogEvent("Error uploading video: ", ex.Message, ex.ToString(),
                EventTypes.Error, false, "");
        }
        return "Failed";
    }
Thomas Andreè Wang
  • 3,379
  • 6
  • 37
  • 53