0

要使用ByteArrayContentfor上传文件,HttpClient我正在将StorageFilea 读入byte数组。该代码在处理图像时没有问题,但它不适用于通过 a 生成AudioVideoCaptureDevice的文件 - 即使文件不是空的,我也仔细检查过。*它将以任何方式访问文件

byte[] data;
StorageFolder folder = ApplicationData.Current.LocalFolder;

try {
    Debug.WriteLine("uploading: "+ fileName);
    StorageFile file = await folder.GetFileAsync("data\\"+LocalPayload);
    var probs = await file.GetBasicPropertiesAsync();
    Debug.WriteLine("path " + file.Path + "" + " size " + probs.Size);

    try
    {

        var stream = await file.OpenReadAsync();
        using (var dataReader = new DataReader(stream))
        {
            Debug.WriteLine("stream size is " + stream.Size);
            data = new byte[stream.Size];
            await dataReader.LoadAsync((uint)stream.Size);
            dataReader.ReadBytes(data);
        }

        Debug.WriteLine("stream was read");
        content = new ByteArrayContent(data);
    }
    catch (System.IO.FileNotFoundException e)
    {
        Debug.WriteLine("file not found", e.ToString());
        return;
    }
    catch (Exception e)
    {
        Debug.WriteLine("could not create byte array for file: " + e.ToString());
        return;
    }

}
catch (Exception e)
{
    Debug.WriteLine("could not read file: " + e.ToString());
    return;
}

这是例外

could not read file: System.ArgumentException: Value does not fall within the expected range.
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at TestApp.Models.Item.<Upload>d__1f.MoveNext()

捕获我第一次用来录制声音的文件

IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
var dataFolder = await applicationFolder.CreateFolderAsync("data", CreationCollisionOption.OpenIfExists);
outputFile = await dataFolder.CreateFileAsync(audioFileName, CreationCollisionOption.ReplaceExisting);
stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite);
await dev.StartRecordingToStreamAsync(stream);

经过一些用户交互后,我正在打电话

await dev.StopRecordingAsync();
Debug.WriteLine("stopped recording");
await stream.FlushAsync();
stream.Dispose();

有什么建议为什么有些文件可以工作而有些文件不能工作?

谢谢!

UPDATE添加了用于创建文件和文件夹源的代码

UPDATE2添加了用于围绕 try catch 读取文件大小的代码

4

1 回答 1

0

问题是由于声音文件的文件名错误。我保存了绝对路径,而不仅仅是上面代码中使用的文件名。

感谢所有试图提供帮助的人!

于 2013-06-19T09:11:27.530 回答