Note that there are some explanatory texts on larger screens.

plurals
  1. POSystem.ArgumentException when reading a file into a byte array
    text
    copied!<p>To upload a file using a <code>ByteArrayContent</code> for <code>HttpClient</code>i'm reading a <code>StorageFile</code> into a <code>byte</code> array. The code is working without problems with images, but its not working for files im generating through a <code>AudioVideoCaptureDevice</code>- even through the files are NOT empty which i double checked. *<em>It'll accessing the file in any way</em>:</p> <pre><code>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; } </code></pre> <p>This is the exception</p> <pre><code>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.&lt;Upload&gt;d__1f.MoveNext() </code></pre> <p>To Capture the file i'm first using to record sound</p> <pre><code>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); </code></pre> <p>and after some user interaction i'm calling</p> <pre><code>await dev.StopRecordingAsync(); Debug.WriteLine("stopped recording"); await stream.FlushAsync(); stream.Dispose(); </code></pre> <p>Any suggestions why some files are working and some are not?</p> <p>Thanks!</p> <p><strong>UPDATE</strong> added code for creating the file and the source of the folder</p> <p><strong>UPDATE2</strong> added code for surrounding try catch to read file size</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload