C# Video through webservice

Viewed 25

I need to send a large video through webservice. I created the function and, because the file is about 2.3 Gb, I split it into 3 parts (1'000'000'000 bytes per part).

When I save from the client the stream received, video doesn't play. I tried also with a small video (197 Mb) and it works correctly.

Here below my server code:

public List<long> GetBigFilePartsNum(int id)
{
    List<long> lRet = new List<long>();

    using (var db = new DatabaseContext("Database"))
    {
        var file = db.Files.FirstOrDefault(f => f.Id == id);

        if (file != null)
        {
            try
            {
                string tempPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"APPTEST\Temp");
                tempPath += new FileInfo(file.FilePath).Extension;

                if (System.IO.File.Exists(tempPath))
                    System.IO.File.Delete(tempPath);

                System.IO.File.Copy(file.FilePath, tempPath);
                //FileStream fs = new FileStream(file.FilePath, FileMode.Open);
                FileStream fs = new FileStream(tempPath, FileMode.Open);
                int parts = (int)Math.Ceiling((double)fs.Length / chunkSize);
                lRet.Add(parts);
                lRet.Add(fs.Length);
                fs.Close();
                return lRet;
            }
            catch { }
        }
    }

    return lRet;
}

int chunkSize = 1000000000;

public Stream GetBigFiledevice(int id, int part)
{
    string tempPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"TESTAPP\Temp");
    tempPath += new FileInfo(file.FilePath).Extension;

    if (System.IO.File.Exists(tempPath))
        System.IO.File.Delete(tempPath);

    System.IO.File.Copy(file.FilePath, tempPath);
    FileStream fs = new FileStream(tempPath, FileMode.Open);

    long startId = (part - 1) * chunkSize;
    long endId = startId + chunkSize > fs.Length ? fs.Length : startId + chunkSize;
    long len = endId - startId;
    byte[] buffer = new byte[len];
    fs.Read(buffer, 0, buffer.Length);
    MemoryStream stMs = new MemoryStream();
    stMs.Write(buffer, 0, buffer.Length);
    stMs.Position = 0;
    fs.Close();
    return stMs;
}

Here my client code:

List<long> list = Program.WebService.GetBigFilePartsNum(1).ToList();
int parts = (int)list[0];
long len = list[1];
string path = Path.Combine(Application.StartupPath, "Temp.mp4");

if (System.IO.File.Exists(path))
    System.IO.File.Delete(path);

var fs = File.Create(path);

for (int i = 1; i <= parts; i++)
{
    long startId = (i - 1) * chunkSize;
    long endId = (startId + chunkSize > len ? len : startId + chunkSize);
    long length = endId - startId;
    Console.WriteLine(String.Format("Part {0} of {1}", i.ToString(), parts.ToString()));
    var str = Program.WebService.GetBigFileDevice(1, i);
    fs.Position = fs.Length;
    str.CopyTo(fs);
    str.Close();
}

fs.Close();

I think the problem is when I do str.CopyTo(fs) with more than one part.

0 Answers
Related