如何在Windows Phone 8中将视频上传为块

时间:2014-04-23 10:28:06

标签: windows-phone-8 uploading

这是我将视频上传为块的代码,但它显示未找到远程服务器。

public void Uploadvideo()
{
    IsolatedStorageFileStream isoVideoFile = new IsolatedStorageFileStream(videoName, ileMode.Open, FileAccess.Read, IsolatedStorageFile.GetUserStoreForApplication());
    if (isoVideoFile != null)
    {
        string FilePath = videoName;
        long Offset = 0;
        int ChunkSize = 2621;
        byte[] Buffer = new byte[ChunkSize];

        try
        {
            long FileSize = isoVideoFile.Length; 
            isoVideoFile.Position = Offset;
            int BytesRead = 0;
            while (Offset != FileSize)
            {
                BytesRead = isoVideoFile.Read(Buffer, 0, ChunkSize);
                if (BytesRead != Buffer.Length)
                {
                    ChunkSize = BytesRead;
                    byte[] TrimmedBuffer = new byte[BytesRead];
                    Array.Copy(Buffer, TrimmedBuffer, BytesRead);
                    Buffer = TrimmedBuffer;
                }

                string url = "http://sample/testService/service.asmx/UploadFileWithOffset?";

                string Params = "FileName=" + videoName + "&buffer=" + Buffer + "&Offset=" + Offset;

                var res = await GetHttpWebRequestAndRespose(url, Params);
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            isoVideoFile.Close();
        }
    }
}

public async static Task<string> GetHttpWebRequestAndRespose(string url, string postData)
{
    var tcs = new TaskCompletionSource<string>();
    if (NetworkInterface.GetIsNetworkAvailable())
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        //request.ContentType = "application/json";
        request.ContentType = "application/x-www-form-urlencoded";
        byte[] requestBody = Encoding.UTF8.GetBytes(postData);

        using (var postStream = await GetRequestStreamAsync(request))
        {
            await postStream.WriteAsync(requestBody, 0, requestBody.Length);
        }

        try
        {
            var response = (HttpWebResponse)await request.GetResponseAsync();
            if (response != null)
            {
                var reader = new StreamReader(response.GetResponseStream());

                string responseString = await reader.ReadToEndAsync();
                tcs.SetResult(responseString);
            }

        }
        catch (WebException we)
        {
            var reader = new StreamReader(we.Response.GetResponseStream());
            string responseString = reader.ReadToEnd();
            tcs.SetResult(responseString);
        }
    }
    else
    {
        MessageBox.Show("Network unavailable, Please try again later.");
    }
    return await tcs.Task;
}

我的代码中有什么错误?
如果有人帮助明白。

0 个答案:

没有答案