在WP8中使用BackgroundUploadAsync上传文件时出错?

时间:2013-07-22 20:35:17

标签: c# upload windows-phone-8 onedrive live-connect-sdk

我想将录制的文件上传到skydrive,我正在使用这些代码

录音;

void StopRecording()
{
    // Get the last partial buffer
    int sampleSize = microphone.GetSampleSizeInBytes(microphone.BufferDuration);
    byte[] extraBuffer = new byte[sampleSize];
    int extraBytes = microphone.GetData(extraBuffer);

    // Stop recording
    microphone.Stop();

    // Create MemoInfo object and add at top of collection
    int totalSize = memoBufferCollection.Count * sampleSize + extraBytes;
    TimeSpan duration = microphone.GetSampleDuration(totalSize);
    MemoInfo memoInfo = new MemoInfo(DateTime.UtcNow, totalSize, duration);
    memoFiles.Insert(0, memoInfo);

    // Save data in isolated storage
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream stream = storage.CreateFile("/shared/transfers/" + memoInfo.FileName))
        {
            // Write buffers from collection
            foreach (byte[] buffer in memoBufferCollection)
                stream.Write(buffer, 0, buffer.Length);

            // Write partial buffer
            stream.Write(extraBuffer, 0, extraBytes);
        }
    }
}

用于上传文件;

async void OnSaveButtonClick(object sender, RoutedEventArgs args)
{
    Button btn = sender as Button;
    MemoInfo clickedMemoInfo = btn.Tag as MemoInfo;
    memosListBox.SelectedItem = clickedMemoInfo;
    if (this.client == null)
    {
        gridSingin.Visibility = Visibility.Visible;
        memosListBox.Visibility = Visibility.Collapsed;
    }
    else
    {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (var fileStream = store.OpenFile(clickedMemoInfo.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                LiveOperationResult res = await client.BackgroundUploadAsync("me/skydrive",
                                                                            new Uri("/shared/transfers/" + clickedMemoInfo.FileName, UriKind.Relative),
                                                                            OverwriteOption.Overwrite
                                                                            );
                InfoText.Text = "File " + clickedMemoInfo.FileName + " uploaded";
            }
        }
    }
}

但我在这里犯了错误

LiveOperationResult res = await client.BackgroundUploadAsync("me/skydrive", new Uri("/shared/transfers/" + clickedMemoInfo.FileName, UriKind.Relative), OverwriteOption.Overwrite);

我收到此错误;

  

System.Windows.ni.dll中发生未处理的“System.Reflection.TargetInvocationException”类型异常

请问你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试上传时无法打开文件。试着这样做。

using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (store.FileExists(fileName))
    {
        client.BackgroundUploadAsync("me/skydrive", new Uri(fileName, UriKind.Relative),
                                        OverwriteOption.Overwrite);

    }
}