将文件读入字节数组时出现System.ArgumentException

时间:2013-06-18 22:02:02

标签: c# wpf windows-phone-8

要使用ByteArrayContent HttpClient上传文件,我正在StorageFile读取byte数组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();

在一些用户互动之后,我正在打电话

{{1}}

为什么有些文件有效,有些文件没有?

谢谢!

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

UPDATE2 为周围的try catch添加了代码来读取文件大小

1 个答案:

答案 0 :(得分:0)

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

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