I have a MemoryStream object that contains the contents of a file. I would like to convert it from a MemoryStream to a FileStream in order to pass it to MS Graph's LargeFileUploadTask () method.
It seems that the method accepts the generic type called "Stream" so I tried using it as is, but it's taking a very long time. In fact I get an HTTP timeout for larger streams.
I have another method similar to this one that also uses the LargeFileUploadTask and trying to upload the same file works - it's using a FileStream (the difference between the two methods is that one writes to a local file, and then opens a filestream before sending to MS Graph, and the one here receives a memorystream as a parameter)
Code
private static async Task<Boolean> UploadInChunksToSharepoint(MemoryStream fileContents, string fileName)
....
// Max slice size must be a multiple of 320 KiB
int maxSliceSize = 320 * 204800 * 4;
var fileUploadTask =
new LargeFileUploadTask<DriveItem>(uploadSession, fileContents, maxSliceSize);
What I've Tried
In reading other posts, I see that there's a WriteTo method in the fileContents object. So i tried something like this:
FileStream fileStream = new FileStream();
fileContents.WriteTo(fileStream);
But... I guess I can't do that because the FileStream doesn't allow you to just initialize without any parameters. If there's a way... I'd like to be able to try this to compare apples to apples between the two methods to see why there's such a time diff.
In the meantime, I'm poking around the rest of the code to see if there might be other deltas that I just missed.
I have a MemoryStream object that contains the contents of a file. I would like to convert it from a MemoryStream to a FileStream in order to pass it to MS Graph's LargeFileUploadTask() method
But the docs you linked simply state that a Stream
is required:
Yes, a FileStream
is a Stream
but a MemoryStream
is also a Stream
and is a "readable, seekable stream" like the docs say is required; it means you can just pass the MemoryStream
you have for the uploadStream
parameter.
Perhaps make sure you've Seek
'd it to Position
0 before you pass it.. If you've just finished writing into a Memorystream, it's Position
ed at the end. If you then pass it to something that will start reading from it, that thing usually reads nothing (because the stream is at the end) - seek the stream back to the start/where you want the upload to begin because you should assume that the thing you give it to will not seek it before reading from it
The OP said the problem was that they forgot to rewind the MemoryStream
before attempting to use it as the source of a HTTP request payload.
@Dai, that's all it was. I reset position to 0. if you want to add as answer... i'll accept
Gladly.
So watch out if you have this:
using( MemoryStream ms = new MemoryStream() )
{
using( StreamWriter wtr = new StreamWriter( ms, false ) )
{
CopyLoremIpsum( wtr );
}
await UploadStreamAsync( ms );
}
The StreamWriter
(or any other writing action) will set the MemoryStream.Position
to the end of the stream, where it cannot be read any further (basically as if you had a cassette tape movie or VHS played past the end-credits).
So make sure you reset .Position
or use .Seek
after writing to it, but before attempting to read from it:
using( MemoryStream ms = new MemoryStream() )
{
using( StreamWriter wtr = new StreamWriter( ms, false ) )
{
CopyLoremIpsum( wtr );
}
ms.Position = 0;
await UploadStreamAsync( ms );
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.