错误在WP8中使用BackgroundUploadAsync上传文件时? [英] Error When uploading file with BackgroundUploadAsync in WP8?

查看:94
本文介绍了错误在WP8中使用BackgroundUploadAsync上传文件时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to upload my recorded file to skydrive and I am using these codes

用于记录;

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'的未处理异常

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll

可以帮我吗?

推荐答案

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

You cannot have the file open when trying to upload. Try doing this.

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

    }
}

这篇关于错误在WP8中使用BackgroundUploadAsync上传文件时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆